Reputation:
I have a main class:
public class Main extends JFrame {
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Main m = new Main();
m.initGUI();
}
});
public void initGUI() {
//add components for this JFrame
//add JPanel with table
//etc..
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
}
then I have a class which extends JPanel:
class CTable extends JPanel {
JTable table;
public void initGUI() {
//add components, table to JPanel etc...
//action listeners to table
}
public void handleTableRowOnClick(String data) {
InfoDialog d = new InfoDialog(data);
//HERE IS MY PROBLEM
//do something else here (THIS SHOULD EXECUTE BUT IT DOESN'T) such as:
String test = "test"
//(IT ONLY EXECUTES AFTER I CLOSE THE DIALOG)
//and I need the ModalityType.APPLICATION_MODAL functionality
}
}
then I have another class:
class InfoDialog extends JDialog {
JComboBox cb;
String data;
public void initGUI() {
//add components such as JComboBox
//etc...
this.setModalityType(ModalityType.APPLICATION_MODAL);
this.setTitle("test");
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public InfoDialog(String data) {
this.data = data;
this.initGUI();
}
}
My question is what is the best method in this situation to make sure InfoDialog instance is in the same event dispatching thread (EDT)?
Thank you for any responses.
Upvotes: 0
Views: 79
Reputation: 347204
The best solution would be to check EventQueue.isDispatchingThread
BEFORE you create the dialog...
public void handleTableRowOnClick(final String data) {
Runnable runner = new Runnable() {
public void run() {
InfoDialog d = new InfoDialog(data);
}
}
if (EventQueue.isDispatchingThread()) {
runner.run();
} else {
EventQueue.invokeLater(runner);
}
}
As I said in you previous question, it should the responsibility of the caller to ensure that the code is executed correctly not your components.
Upvotes: 2