[英]Autoclose JOptionPane.showconfirmDialog with a timer
I want to create a JOptionPane.showConfirmDialog
with a timer. 我想创建一个带有计时器的JOptionPane.showConfirmDialog
。 And the default option will be Exit. 默认选项是退出。 But if i click on Yes option it should continue the work , and if I click No Option it should exit. 但是,如果我单击“是”选项,它应该继续工作,如果我单击“否选项”,它应该退出。 If I don't click on any option it should automatically exit from the code. 如果我不单击任何选项,它将自动从代码中退出。
I tried the below sample code. 我尝试了以下示例代码。 It is partially working. 它正在部分工作。 But the problem is I cannot simulate the Yes/No Option. 但是问题是我无法模拟“是/否”选项。 In any case it is exiting from the code with the YES option. 无论如何,它都是使用YES选项从代码中退出的。
Although this code is taken from one of the thread, but there the implementation is different. 尽管此代码是从线程之一中获取的,但是在实现上却有所不同。 I just modified the code according to my need. 我只是根据需要修改了代码。 Please find the below code: 请找到以下代码:
public class TestProgress {
public static void main(String[] args) {
final JOptionPane msg = new JOptionPane("Database Already Exist. Do you want to continue...?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
final JDialog dlg = msg.createDialog("Select Yes or No");
final int n = msg.YES_NO_OPTION;
dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(msg.YES_OPTION==n){
System.out.println("Continue the work.. "); // should not exit
}
else if(msg.NO_OPTION==n)
dlg.setVisible(false);
System.exit(1);
}
}).start();
dlg.setVisible(true);
System.out.println("Outside code.");
}
}
What else do I need to do, to make it work correctly? 我还需要做什么才能使其正常工作?
The autoclosing dialog of JOptionPane JOptionPane的自动关闭对话框
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
public class TestProgress {
public static void main(String[] args) {
final JOptionPane msg = new JOptionPane("Database Already Exist. Do you want to continue...?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
final JDialog dlg = msg.createDialog("Select Yes or No");
dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dlg.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
super.componentShown(e);
final Timer t = new Timer(5000,new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dlg.setVisible(false);
}
});
t.start();
}
});
dlg.setVisible(true);
System.out.println("Outside code.");
}
}
UPDATE: 更新:
Use extended constructor where you can pass initial option and specify NO
as default 使用扩展构造函数,您可以在其中传递初始选项并将默认值指定为NO
JOptionPane(Object message, int messageType, int optionType,
Icon icon, Object[] options, Object initialValue)
a completed answer for this question. 这个问题的完整答案。
public final static boolean showConfirmDialogWithTimeout(Object params, String title, int timeout_ms) {
final JOptionPane msg = new JOptionPane(params, JOptionPane.WARNING_MESSAGE, JOptionPane.CANCEL_OPTION);
final JDialog dlg = msg.createDialog(title);
msg.setInitialSelectionValue(JOptionPane.OK_OPTION);
dlg.setAlwaysOnTop(true);
dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dlg.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
super.componentShown(e);
final Timer t = new Timer(timeout_ms, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dlg.setVisible(false);
}
});
t.start();
}
});
dlg.setVisible(true);
Object selectedvalue = msg.getValue();
if (selectedvalue.equals(JOptionPane.CANCEL_OPTION)) {
return false;
} else {
return true;
}
}
// example usage
String message = "The earth will explode in 10 seconds. Select CANCEL if you won't.";
JLabel lbmsg = new JLabel(message);
boolean result = showConfirmDialogWithTimeout(lbmsg, "Shutdown Warning", 10 * 1000);
if (result == false) {
Utils.showMessage(message + " cancel is selected");
}
else {
Utils.showMessage(message + " timeout or okay is selected");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.