Search code examples
javaswingappletmodal-dialogjdialog

Modal dialog hide behind Main Frame after swich focus


I have a swing application, basically, a main frame that could pop up a modal dialog. When the modal dialog is showing, if I switch to another window, like firefox. And then switch back to the swing application. The JDialog is not in front any more.

I don't want to set the dialog AlwaysOnTop to be true. because then the dialog will be on top of all windows include windows in other process.

So what should I do so that when I swich back, the modal dialog still on top?

BTW: it is a Applet, so the main frame is actually be set in this way:

private static Frame findParentFrame(Container owner){
    Container c = owner;
    while(c != null){
        if (c instanceof Frame)
            return (Frame)c;
        c = c.getParent();
    }
    return (Frame)null;
}

Solution

  • I am not sure if the modality of the dialog is the key issue here. I have tested this behaviour and the dialog always popups on the front when the app is maximised independent of it being modal.

    import java.awt.event.ActionEvent;
    import javax.swing.JApplet;
    import javax.swing.SwingUtilities;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.Frame;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    
    public class AppletTest extends JApplet
            implements ActionListener
    {
        private static final long serialVersionUID = 1L;
        private Frame findParentFrame()
        {
            Container c = this;
            while(c != null)
            {
                if(c instanceof Frame)
                    return (Frame) c;
    
                c = c.getParent();
            }
            return (Frame) null;
        }
        private void createGUI()
        {
            Container content = getContentPane();
            content.setBackground(Color.white);
            content.setLayout(new FlowLayout());
            content.add(new JButton("Button 1"));
            content.add(new JButton("Button 2"));
            content.add(new JButton("Button 3"));
            JDialog d = new JDialog(findParentFrame());
            d.setModal(true);
            d.setVisible(true);
        }
    
        public void init()
        {
            try
            {
                SwingUtilities.invokeAndWait(new Runnable()
                {
                    public void run()
                    {
                        createGUI();
                    }
                });
            }catch(Exception e)
            {
                System.err.println("createGUI didn't successfully complete");
            }
        }
    
        @Override
        public void actionPerformed(ActionEvent e)
        {
        }
    }
    

    Check out the example I provided. You can comment the line with d.setModal(true); and the result will be exactly the same. I would suggest for you to check your code once more or show it to us as it seems that you might have missed something there.

    PS: I found on the web some other hack-like solution http://www.codeguru.com/forum/showthread.php?t=41536 I would still focus on checking your code though.

    Oi & Good luck, Boro.