[英]setting Swing JDialog to non modal
I'm having an issue with setting a JDialog to non-modal. 我在将JDialog设置为非模态时遇到问题。 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.
我尝试使用SwingUtilities.invokeLater(),但顾名思义,它是在完成主线程工作之后被调用的。 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.
(我也尝试了invokeAndWait,但由于无法从主线程调用它,我不得不创建一个新线程,因此结果几乎相同。
Do you have any suggestions? 你有什么建议吗?
You need to start your code process before showing the modal JDialog
, and then show the dialog. 在显示模式
JDialog
之前,需要启动代码过程,然后显示对话框。 You can perhaps use a background thread if the validate method will take a long time. 如果validate方法需要很长时间,则可以使用后台线程。 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.