I want to create a JOptionPane.showConfirmDialog
with a timer. 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.
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?