8

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Codester89
  • 93
  • 1
  • 2
  • 5
  • Is this what you're looking for? http://stackoverflow.com/questions/7787499/how-can-i-dismiss-one-joptionpane-upon-emergence-of-another-joptionpane-in-the-g – Georgian Aug 07 '13 at 14:08
  • typically (though not necessarily), an optionpane is shown in a modal JDialog, so there is at most one optionPane open. – kleopatra Aug 07 '13 at 15:00
  • @GGrec - That response is on the right track, but I find looping through the windows, as kleopatra suggested, to work much better in my case. Thanks! – Codester89 Aug 07 '13 at 18:39

2 Answers2

18

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;
    }
};
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • 1
    Very interesting way of achieving the goal :-) Looks more promising than my own approach :-) – nIcE cOw Aug 07 '13 at 15:33
  • yours 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 – kleopatra Aug 07 '13 at 15:40
  • I 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 :-) – nIcE cOw Aug 07 '13 at 15:51
  • please why `if (window instanceof JDialog) {` isn't `instanceof JOptionPane` directly, because theoretically only one `JOptionPane` can be visible in current time, if passed then you have to create a new loop, and loop inside repeatly, second time – mKorbel Aug 07 '13 at 16:28
  • @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 – kleopatra Aug 07 '13 at 16:33
  • @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. – Codester89 Aug 07 '13 at 18:32
1

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);
Joshua
  • 818
  • 11
  • 12