JOptionPain – JOptionPane hidden behind always on top window crashes Swing UI

nb. this is now bug id 6690019

There is a bug in the java bug database (6519416) related to a similar issue, but it is closed and not fixed – perhaps the reviewer didn’t realise how serious this can be. I think this needs to be looked at again, since it has caused several terminal crashes in production systems I have been working on.

The problem is to do with the use of the ‘always on top’ feature for java.awt.Window. If JFrames in the application are set to always on top, a JOptionPane can be obscured by an always on top frame, and hence be inaccessible to the user. Since the JOptionPane is modal, you can’t interact with or move the always on top frame – so there is no way to reveal the JOptionPane in order to dismiss it. The whole UI just hangs. You can’t even alt-tab to reveal the option pane. The only way out is to kill the process.

There is no problem with the always on top frame is the parent of the JOptionPane – the problem only occurs if the parent is a normal frame, or the JOptionPane has a null parent set.
The following code replicates the problem

public class TestJOptionPaneAlwaysOnTopBug
{
    public static void main(String[] args) {
        SwingUtilities.invokeLater(
                new Runnable() {
                    public void run()
                    {
                        JFrame alwaysOnTopFrame = new JFrame("Test JOptionPane");
                        alwaysOnTopFrame.setSize(600, 400);
                        centreFrame(alwaysOnTopFrame);
                        alwaysOnTopFrame.setAlwaysOnTop(true);
                        alwaysOnTopFrame.setVisible(true);

                        JFrame normalFrame = new JFrame();
                        normalFrame.setSize(800, 600);
                        centreFrame(normalFrame);
                        normalFrame.setVisible(true);

                        JOptionPane.showMessageDialog(normalFrame, "Can't you see me?");
                    }
                });
    }

    private static void centreFrame(JFrame myFrame)
    {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (d.width - myFrame.getWidth()) / 2;
        int y = (d.height - myFrame.getHeight()) / 2;
        myFrame.setLocation(new Point(x, y));
    }
}

There is a workaround, described in the bug report above, which involves creating the dialog for the JOptionPane explicitly and setting it to be always on top before it is displayed. This seems to fix things, at least on the Windows platform. However, if you use the standard JOptionPane static methods to show a dialog, always on top is not set. I think it possibly should be – it seems unlikely that one would ever want to show an option pane which is not on the top. Perhaps there is a corner case here which is not obvious?


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

One Response to “JOptionPain – JOptionPane hidden behind always on top window crashes Swing UI”

  1. Sun accepted my bug report for this – feels like winning the lottery

    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6690019

Leave a Reply