Can i run 2 GUI actions in the EDT?
In a JDialog, when user clicks a JButton i want to execute 2 GUI actions in the EDT :
- Showing another small JDialog with a busy icon in it to tell the user "Please wait while th开发者_如何学Ce wrong process ends".
- 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 ?
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.
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.
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.
精彩评论