Closing A JOptionPane Programmatically
Solution 1
Technically, you can loop through all windows of the application, check is they are of type JDialog and have a child of type JOptionPane, and dispose the dialog if so:
Action showOptionPane = new AbstractAction("show me pane!") {
@Override
public void actionPerformed(ActionEvent e) {
createCloseTimer(3).start();
JOptionPane.showMessageDialog((Component) e.getSource(), "nothing to do!");
}
private Timer createCloseTimer(int seconds) {
ActionListener close = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Window[] windows = Window.getWindows();
for (Window window : windows) {
if (window instanceof JDialog) {
JDialog dialog = (JDialog) window;
if (dialog.getContentPane().getComponentCount() == 1
&& dialog.getContentPane().getComponent(0) instanceof JOptionPane){
dialog.dispose();
}
}
}
}
};
Timer t = new Timer(seconds * 1000, close);
t.setRepeats(false);
return t;
}
};
Solution 2
This code gotten from https://amp.reddit.com/r/javahelp/comments/36dv3t/how_to_close_this_joptionpane_using_code/ seems to be the best approach to me. It involves Instantiating the JOptionPane class rather that using the static helper methods to do it for you. The benefit is you have a JOptionPane object that you can dispose when you want to close the dialog.
JOptionPane jop = new JOptionPane();
jop.setMessageType(JOptionPane.PLAIN_MESSAGE);
jop.setMessage("Hello World");
JDialog dialog = jop.createDialog(null, "Message");
// Set a 2 second timer
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (Exception e) {
}
dialog.dispose();
}
}).start();
dialog.setVisible(true);
Codester89
Updated on June 25, 2022Comments
-
Codester89 almost 2 years
I am working on a project in which I would like to close a generic JOptionPane programmatically (by not physically clicking on any buttons). When a timer expires, I would like to close any possible JOptionPane that may be open and kick the user back to the login screen of my program. I can kick the user back just fine, but the JOptionPane remains unless I physically click a button on it.
I have looked on many sites with no such luck. A doClick() method call on the "Red X" of the JOptionPane does not seem possible, and using JOptionpane.getRootFrame().dispose() does not work.
-
nIcE cOw almost 11 yearsVery interesting way of achieving the goal :-) Looks more promising than my own approach :-)
-
kleopatra almost 11 yearsyours was correct as well (was distracted before having the opportunity to upvote :-) In all real-world contexts I would expect some keeping track of which timer belongs to which optionPane
-
nIcE cOw almost 11 yearsI was matching mine approach with yours, then I found, that I forgot to shut the timer down anywhere :( and moreover in this presented approach, there is no need to convert one thingy to another, and everything works in accordance :-)
-
mKorbel almost 11 yearsplease why
if (window instanceof JDialog) {
isn'tinstanceof JOptionPane
directly, because theoretically only oneJOptionPane
can be visible in current time, if passed then you have to create a new loop, and loop inside repeatly, second time -
kleopatra almost 11 years@mKorbel ?? - don't understand what you are saying: JOptionPane is not a topLevel container, so it wouldn't make sense to type-check the items in the array of top-level containers against JOptionPane. Why would I loop again? I'm just going through all and closing all
-
Codester89 almost 11 years@kleopatra - This is a very intelligent and thorough answer. I was able to implement the code into my existing Timer and follow along with your logic to dispose of any JOptionPanes still on the screen. Just what I was looking for, thank you very much! Something of interest, I did end up using just...
Window[] windows = Window.getWindows(); for (Window window : windows) { if (window instanceof JDialog) { window.dispose(); } }
...since I realized I had a few custom JDialogs in my program that were not JOptionPanes.