Reputation: 33
I'm having an issue with setting a JDialog to non-modal. I need to display a pop-up while not blocking the rest of the application. I tried using SwingUtilities.invokeLater() but as the name suggests it was invoked much later, after the work of the main thread is done. To simplify, here's my code:
BufferedReader reader = new BufferedReader(new FileReader(log));
JLabel validator = new JLabel("Validating - please wait");
JOptionPane pane = new JOptionPane(validator, JOptionPane.INFORMATION_MESSAGE,JOptionPane.NO_OPTION,null, new String[]{"Close"});
final JDialog dialog = pane.createDialog(null, "title");
dialog.setModal(false);
dialog.setVisible(true);
dialog.setVisible(true);
writer = validate(reader);
dialog.dispose();
The dialog shows up but it's empty. If I use it as modal, it shows up fine. I tried using it with certain variations, such as this:
JLabel validator = new JLabel("Validating - please wait");
JOptionPane pane = new JOptionPane(validator, JOptionPane.INFORMATION_MESSAGE,JOptionPane.NO_OPTION,null, new String[]{"Close"});
final JDialog dialog = pane.createDialog(null, "Validation in progress");
Runnable run = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
dialog.setModal(false);
dialog.setVisible(true);
}
};
SwingUtilities.invokeLater(run);
writer = validate(reader);
But as I said, the dialog is invoked much too late for me. (I also tried invokeAndWait but seeing as I can't invoke it from the main thread I had to create a new one so the result was pretty much the same.
Do you have any suggestions?
Upvotes: 2
Views: 2012
Reputation: 285430
You need to start your code process before showing the modal JDialog
, and then show the dialog. You can perhaps use a background thread if the validate method will take a long time. Something like this:
BufferedReader reader = new BufferedReader(new FileReader(log));
JLabel validator = new JLabel("Validating - please wait");
JOptionPane pane = new JOptionPane(validator, JOptionPane.INFORMATION_MESSAGE,JOptionPane.NO_OPTION,null, new String[]{"Close"});
final JDialog dialog = pane.createDialog(null, "title");
dialog.setModal(true);
SwingWorker myWorker = new SwingWorker<String, Void>() {
public void doInBackground() {
// do long running process
// perhaps including
writer = validate(reader);
// ....
return yourString;
}
public void done() {
update JLabel
dispose of dialog here!
}
};
myWorker.execute();
dialog.setVisible(true);
Upvotes: 2