paranoia25
paranoia25

Reputation: 626

Modal dialog not always on top of an undecorated JFrame when another JFrame is visible

I have a weird problem with modal dialogs and undecorated JFrame.

If I create a main undecorated JFrame then I display a modal dialog thanks to the JOptionPane, everything goes well. The modal dialog stays always on top and I can't click on the main fame.

But, if create another JFrame (or another JDialog), the modal dialog still prevent me to interact with the main frame, but now the modal dialog is not always on top and go underneath the main frame when I click on it.

This problem doesn't happen:

EDIT

I use jdk1.7.0.0_09 on Linux Suse.But I have the same result with jre 1.6.0_32

The code I used to test:

 public static void main(String[] args) {
    // creates main frame and set visible to true
    final JFrame mainFrame = new JFrame();
    mainFrame.setUndecorated(true); // if I comment this line, everything goes well
    mainFrame.add((new JPanel()).add(new JButton("OK")));
    mainFrame.setSize(new Dimension(500, 500));
    mainFrame.setVisible(true);
    // creates a dialog and set visible to true
    JFrame anotherFrame = new JFrame();
    anotherFrame.setVisible(true); // or if I comment this line, everything goes well too
    // display a modal dialog
    JOptionPane.showMessageDialog(mainFrame, "A message");
}

Upvotes: 1

Views: 1940

Answers (1)

mKorbel
mKorbel

Reputation: 109823

But, if create another JFrame (or another JDialog), the modal dialog still prevent me to interact with the main frame, but now the modal dialog is not always on top and go underneath the main frame when I click on it.

  • not true at all, both are not accesible untill JOptioPane is visible

  • JOptionPane or JDialod.setModal(true) block mouse or key events to the alll windows invoked from currnet JVM

  • there must be something else that isn't clear from your question, rest of code, minor could be Java version and Native OS

enter image description here

code for Java6 (winxp), works for me on Win7 / Java7(x.x_011)

import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Main {

   private JFrame mainFrame = new JFrame();
   private JFrame anotherFrame = new JFrame();

    public Main() {
        mainFrame.setUndecorated(true);
        mainFrame.add((new JPanel()).add(new JButton("OK")));
        mainFrame.setSize(new Dimension(100, 60));
        mainFrame.setVisible(true);
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        anotherFrame.setVisible(true);
        anotherFrame.setLocation(110, 0);
        anotherFrame.setSize(new Dimension(100, 60));
        anotherFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JOptionPane.showMessageDialog(mainFrame, "A message");
    }


    public static void main(String[] args) {
       java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Main main = new Main();
            }
        });
    }
}  

Upvotes: 4

Related Questions