-1

I have 3 dialog that gets displayed together in my project.

  • First dialog is modeless with setVisibleOnTop(false).
  • Second dialog is modeless with setVisibleOnTop(true).
  • Third dialog is Application Modal with setVisibleOnTop(true).

Now the issue is :

  1. Ideally when there is a dialog "third" opened with APPLICATION_MODAL property then no other JComponent should accept the click. This works fine with java 1.7.
  2. With java 1.6 is I click on dialog "one" then dialog "second" goes at the back of dialog "one". Whereas dialog "third" is still opened.

Now the question is:

  • Why dialog "one" comes in front when there is an APPLICATION_MODAL dialog (third) opened?
  • Why second dialog with property setAlwaysOnTop(true) goes at back?
  • I believe this is a issue with java 1.6. Does anyone know about this?
  • Is this bug documented somewhere?

Sample Code:

import java.awt.Frame;

import javax.swing.JDialog;
 class MyDialog1 extends JDialog {

     MyDialog1 ()
    {
        super();
        super.setVisible(false);
        setTitle("one");
    }


}


 class MyDialog2 extends JDialog {

     MyDialog2 ()
        {
         super(null,ModalityType.MODELESS);
            setAlwaysOnTop(true);
            setTitle("second");

        }

    }


 class MyDialog3 extends JDialog {

     MyDialog3 ()
    {
        super(new Frame(),ModalityType.APPLICATION_MODAL);
        setTitle("third");
        setAlwaysOnTop(true);
        super.setVisible(false);

    }

}

public class ModalityIssue {

    public static void main(String args[])
    {
        MyDialog1 d1=new MyDialog1();
        d1.setSize(600, 600);

        MyDialog2 d2=new MyDialog2();
        d2.setSize(500, 500);

        MyDialog3 d3=new MyDialog3();
        d3.setSize(400, 400);

        d1.setVisible(true);
        d2.setLocationRelativeTo(d1);
        d2.setVisible(true);
        d3.setLocationRelativeTo(d2);
        d3.setVisible(true);
    }
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Onki
  • 1,879
  • 6
  • 38
  • 58

1 Answers1

1
  • dont to use awt components

  • JFrame ignores alwaysOnTop and modality,

  • use Initial Thread,

  • important is code ordering too

  • you can to use aplication modality (seems like as better, but nobody knows if meets with your requirement/s)

  • MyDialog1 always flashing (MyDialog2 is painted before MyDialog1, then jumps behing MyDialog2, standard users can't catch that), maybe there aren't any JComponents added to any of Top-Level Containers in the current JVM

for example (Java 1.6.21 / 025 / 031, the same corerctly works in 1.7.04 and 1.8.60 / 66 / win10)

import java.awt.Dialog.ModalityType;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

class MyDialog1 extends JDialog {

    public MyDialog1() {
        super(null, ModalityType.MODELESS);
        setAlwaysOnTop(true);
        setTitle("fist");
    }
}

class MyDialog2 extends JDialog {

    public MyDialog2() {
        super(null, ModalityType.MODELESS);
        setAlwaysOnTop(true);
        setTitle("second");
    }
}

public class ModalityIssue {

    private JFrame frame = new JFrame();
    private MyDialog1 d1 = new MyDialog1();
    private MyDialog2 d2 = new MyDialog2();

    public ModalityIssue() {
        frame.setTitle("third");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.setLocation(100, 100);
        frame.setVisible(true);

        d1.setSize(400, 300);
        d1.setLocation(200, 200);
        d1.setVisible(true);

        d2.setSize(400, 300);
        d2.setLocation(300, 300);
        d2.setVisible(true);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ModalityIssue();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319