1

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:

  • if the main frame is decorated
  • or if the second frame is not visible

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");
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
paranoia25
  • 626
  • 1
  • 5
  • 23

1 Answers1

4

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();
            }
        });
    }
}  
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I use your code and I have always the behavior I described previously. If I click on the main frame the dialog goes underneath. I use jdk1.7.0.0_09 on Linux Suse.But I have the same result with jre 1.6.0_32 – paranoia25 Feb 01 '13 at 13:42
  • there are a few **nix/Linux users, I edited tags and add Java & Native OS to the question body, have to wait – mKorbel Feb 01 '13 at 13:52
  • Ok, thx. I made some tests. The problem seems to come from the Window Manager. With the Metacity I have the problem, but I haven't with Kwin. Don't know why... – paranoia25 Feb 01 '13 at 14:08