JOptionPane.showMessageDialog Query?
This is the Scenario.
I have Code which intiates a Alram when an error is encountered.
AudioAlarm t = new AudioAlarm(song);
try {
Thread.sleep(2000);
} catch (Interrupte开发者_如何学GodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Awake");
t.start();
setRunnung(true);
JOptionPane.showMessageDialog(null, "Alarm ...", "Alarm", JOptionPane.OK_OPTION);
AudioAlarm.setLoop(false);
System.out.println("Alarm Acknowledged ...");
I would like to re-design this logic in this manner,
If the Alarm is unacknowledged by the user over a period of time say 2 min, it turn off and message msg dialog should disappear.
How can I Obtain this?
I am able to Stop the Alram, but unable to dispose the dialog without the user pressing "OK"
To do what you want, you should:
- Create a
JOptionPane
instance using one of its constructors - Call
createDialog
on this option pane to get a dialog containing this option pane - Use a
javax.swing.Timer
instance in order to fire an action event after 2 minutes - add an action listener to this timer which would close the dialog containing the option pane
- show the dialog containing the option pane.
I do not know if what you are after can be done, but can't you instead replicate the JOptionPane as a JFrame and dispose of that one? You can find how to close a JFrame on this Previous SO Post:
If you want the GUI to behave as if you clicked the "X" then you need to dispatch a windowClosing Event to the Window. The "ExitAction" from Closing An Application allows you to add this functionality to a menu item or any component that uses Actions easily.
精彩评论