Reputation: 161
I am trying to create a dialog to get user information for a database plugin, and I need to collect multiple fields and stylize the prompt to appear a certain way. I am using a JDialog to do so (I am replacing a pre-existing dialogue to do so, which only collected one field). The new dialog constructs fine and appears, but then freezes (becomes unresponsive) along with the database software that the java plugin I'm coding is in.
For reference, here is the old dialog code that was being used, which worked perfectly and returned an input field without any problems (it contains a bunch of localized strings, but I left them in there for the sake of completion):
InputDialog dlg = new InputDialog(window.getShell(), rb.getString("com.transcendata.ipemsoa.MainAsync.lblTitle"), rb.getString("datasetName"),rb.getString("DefaultDatasetName"), null);
This is the code I am trying to replace it with:
Object multiTest[] = { "Top Test", new JButton("Some Button"), new JSlider(), "Bottom Test"};
JOptionPane optionPane = new JOptionPane();
optionPane.setMessage(multiTest);
optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
JDialog dialog = optionPane.createDialog(null, "Width 100");
dialog.setVisible(true);
The first argument will not accept "window.getShell()" as a parent argument (it is not of the component type and cannot be typecasted into one), and I'm not certain how to parent it to the database software as a parent or if that would even address the problem. Is there an alternative solution, or do I need to invoke JDialog differently to prevent it from halting the program?
Upvotes: 2
Views: 973
Reputation: 3720
.setVisible(true) is a blocking call. It will occupy the thread it is called from until the dialog is closed. You need to run the UI elements on the Swing EDT (Event Dispatch Thread). I would link the official tutorial on it, but this site is anal about outside links, and the read cannot be condensed.
Do this:
SwingUtilities.invokeLater(
new Runnable() {
@Override public void run() {
dialog.setVisible(true);
}
}
);
Upvotes: 3