-1

I use SwingWorkers to fill several JTables with data from a mysql-database and use following code:

jT.setModel(DbUtils.resultSetToTableModel(results))

This works great. But right after each JTable-Model is set I need to execute different methods (methods may differ for each JTable) which e.g. put the row-count of a JTable into a JLabel or calculate sums of certain columns and put that sum into another JLabel...

My Problem:

I want the GUI to stay responsive (therefore the use of SwingWorkers to get the Data in the background), but I need to find a way to somehow "listen" to changes of the jTable (so I can execute the methods AFTER the Table is refreshed). I can't use the obvious solution to make different SwingWorker-classes for each JTable (which include the respective method of that JTable), because I need a general solution.

I already thought about putting each individual method into a TableModelListener and to add that to each respective JTable, so I tested following:

jT.getModel().addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
     System.out.println("TableModelEvent: "+e);
     // Probably I could add this jTable's method right here?

  }
});

But I never get any Output...

I also tested the swingworker's .get()-method to wait for the set of the model (see first line of code) and then execute the method, but that lead to the same situation where I started from (GUI freezes and waits for execution of the swingworker... so no gain by using a swing worker..).

Any ideas?

SEA2605
  • 21
  • 4
  • you have to explain if you want to replace whole XxxTableModel - e.g. repacing required to add all setting again or only changes betweens XxxTableModel and Databases - you can to hold selection etc... – mKorbel Oct 09 '15 at 16:29

1 Answers1

1

The TableModelListener should work, and why might be the subject of another question, where you post your minimal example program, but one thing that likely will work in lieu of the TableModelListener is to use a PropertyChangeListener, add it to your SwingWorker before calling execute, and listen for changes to the SwingWorker state property. The new value you want to listener for is SwingWorker.StateValue.DONE.

  mySwingWorker.addPropertyChangeListener(new PropertyChangeListener() {

     @Override
     public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("state")) {
           if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
              // TODO: code that you want to call when SwingWorker is done
              // **including** calling get() on the SwingWorker itself
              // so you can trap and respond to exceptions thrown during its run.
           }
        }
     }
  });
  mySwingWorker.execute();
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    @Pete [little bit disagree](http://stackoverflow.com/a/6186188/714968), everythig is about the desing == but I've notorious known about linear coding, explainations 1. all bug, mistakes in code(logics) to prevents ends SwingWorker in done() - then the PropertyChangeListener never to notify about DONE, 2. PropertyChangeListener isn't desingated to propagate that SwingWorker ends in done() 3. (can be interpreted that hasn't direct association) together with Future two unmanageble, uncontrolable APIs, anyway + good 1 answer as always – mKorbel Oct 09 '15 at 16:26
  • @mKorbel: thanks for the constructive comments. Please post an alternative answer. – Hovercraft Full Of Eels Oct 09 '15 at 16:37
  • :-) I think have to wait for OPs further details about the question – mKorbel Oct 09 '15 at 17:40
  • This solved my problem mostly! thx! But now I got another related problem: If the user enters something into a jtextfield (which has a keyreleased-listener starting the swingwoker) sometimes the last two-three entered key presses won't trigger the key released-listener... So e.g. if the user searches for "123" sometimes (especially if entered fast) my swing worker will only search for "12"... – SEA2605 Oct 10 '15 at 01:48
  • @SEA2605: **NEVER** use a KeyListener on a text component such as a JTextField, **EVER**. Use either a DocumentListener or a DocumentFilter for most purposes, depending on if you want to capture input before or after the component registers and displays the text. For this I'd use a DocumentListener since you're not changing the text in the JTextField. – Hovercraft Full Of Eels Oct 10 '15 at 01:52
  • thanks, now all key entries are correctly recognized. But that sadly still didn't solve my issue. I will ask another question for that issue. – SEA2605 Oct 10 '15 at 02:56
  • @SEA2605: or improve this question and create and post a [mcve]. – Hovercraft Full Of Eels Oct 10 '15 at 02:56