Peter
Peter

Reputation: 123

JDialog.setAlwaysOnTop(true) brings all dialogs to the front under Windows

I create two dialogs:
DialogA: setVisible(true) called only once.
DialogB: setVisible(true) and setAlwaysOnTop(true) called every 1,5 sec

Windows: Each call to dialogB.setAlwaysOnTop(true) brings dialogA AND dialogB to the front.
OSX: Each call to dialogB.setAlwaysOnTop(true) brings only dialogB to the front. (Expected Behaviour)

Test Case (Windows):

1 I start the application from my IDE.
2 I see DialogA.
3 I click in the IDE and DialogA disappears.
4 After one second DialogA and DialogB will show up.
5 I click in the IDE and DialogA and DialogB disappears. GOTO 4

Expected Behaviour(OSX):
4. After one second DialogB will show up.
5. I click in the IDE and DialogB disappears. GOTO 4

Question:
How do I get the expected behaviour under Windows?

import javax.swing.JDialog;
import javax.swing.JLabel;
public class JDialogSetAlwaysonTopTEST
{
public static void main(String[] p_Strings)
{
    final JDialog dialogA = new JDialog();
    dialogA.setLocation(0, 0);
    dialogA.add(new JLabel("DialogA: Click on the overlapped   window"));       
    java.awt.EventQueue.invokeLater(new Runnable() {           
        public void run() {
            dialogA.pack();
            dialogA.setVisible(true);
        }
    });

    try {Thread.sleep(3000);} catch (InterruptedException e){}

    final JDialog dialogB = new JDialog();
    dialogB.setLocation(70, 70);
    dialogB.add(new JLabel("DialogB:  Do you see DialogA?"));

    java.awt.EventQueue.invokeLater(new Runnable() {           
        public void run() {
            dialogB.pack();
            dialogB.setVisible(true);
        }
    });

    while(true)
    {
        java.awt.EventQueue.invokeLater(new Runnable()  {                  
            public void run() {
                dialogB.setAlwaysOnTop(true);  //prerequisite
                dialogB.setVisible(true);
                dialogB.setAlwaysOnTop(false); //prerequisite
            }
        });         
        try {Thread.sleep(1500);} catch (InterruptedException e){}
    }       
}
}

Upvotes: 4

Views: 3297

Answers (1)

Peter
Peter

Reputation: 123

I found a "dirty" solution to my problem.

final JDialog dialogA = new JDialog(new JFrame());
...
final JDialog dialogB = new JDialog(new JFrame());

If each dialog has an independent owner dialogB.setAlwaysOnTop(true), dialogB.setVisible(true) does not effect dialogA

Upvotes: 1

Related Questions