tags:

views:

64

answers:

3

In a JDialog, when user clicks a JButton i want to execute 2 GUI actions in the EDT :

  1. Showing another small JDialog with a busy icon in it to tell the user "Please wait while the wrong process ends".
  2. Inserting a big number of records in a JTable.

When i try to execute both actions the "please wait" dialog blocks the inserting process, as expected.

As you see both actions must be done in EDT ... so is there a solution for this ?

+1  A: 

No, both actions should not be executed in the EDT.

Your records should not be inserted in the JTable, but rather in its TableModel, triggering update events. This way, you can easily have the table updated while your dialog is shown.

Once the table model is updated, fire an event to ensure table is repainted, and it will work.

Riduidel
Beat me by a few seconds.
Paul Tomblin
Thanks Riduidel ... Paul, i did not know who answered the question first, so thanks for your comment :)
Brad
+1  A: 

The second thing does not need to be done in the EDT. Spawn off a thread to add the items to the JTable's model, but have that thread occasionally use SwingWorker.invokeLater() to fire off "fireTableDataChanged" events.

Paul Tomblin
Did you mean `SwingUtilities.invokeLater()`, a cover for `EventQueue.invokeLater()`, here and in later comments?
trashgod
That's the one. When you've done as many programming languages and APIs as I have, all the names run into each other.
Paul Tomblin
+1  A: 

Most TableModels, for example the DefaultTableModel, invoke the fireXXX methods as soon as the model is updated so yes you want the updating of the model to be done on the EDT so the table gets repainted properly.

"Please wait while the wrong process ends".

Use an indeterminate JProgressBar

then you can update the model as required without it locking.

camickr
This is incorrect. It's standard to update the table model in a non-EDT thread, and then call "fireTableDataChanged" in the EDT using SwingWorker.invokeLater.
Paul Tomblin
@Paul Tomblin: I think @camickr is correct about the behavior of `DefaultTableModel`, but you are correct about practical implementations of `TableModel`, including concrete subclasses of `AbstractTableModel`.
trashgod
@Paul Tomblin, As I understand MVC it is the responsibility of the Model to notify the View when data in the Model changes. I believe all the "default Models" in the JDK (ie models for JTable, JTree, JList...) do invoke the fireXXX method that have been implemented in the related Abstract Model. If you create a custom model then you are free to not notify the View when the Model changes but I believe this breaks MVC.
camickr
Explain to me again why calling fireTableDataChanged in the EDT using SwingWorker.invokeLater is breaking MVC?
Paul Tomblin
Because the Model is not notifying the View. Your method is 1) updating the Model and then 2) notifying the View. Therefore if the programmer forgets the 2 steps the View will not change. My understanding of MVC is that the Model should notify the View directly.
camickr
I agree with Camickr ... But what i actually did is emptying the JTable model, and filling it from the beginning. When i did that i did not need to notify the View with the change. It was updated automatically !
Brad