4

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){}
    }       
}
}
Vadzim
  • 24,954
  • 11
  • 143
  • 151
Peter
  • 123
  • 2
  • 9
  • Don't know if it will make a difference, but you could start by respecting Swing's thread policy, and only use Swing components from the EDT. – JB Nizet Jan 19 '12 at 15:27
  • 3
    You're still creating and modifying swing components in the main thread. Everything except the sleeping must be done on the EDT. – JB Nizet Jan 19 '12 at 15:54
  • You are right! But the result remains the same. – Peter Jan 19 '12 at 15:59
  • @JBNizet: I think the code is ok considering EDT [link]http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html – Peter Jan 19 '12 at 16:14
  • No it isn't. Read http://docs.oracle.com/javase/6/docs/api/javax/swing/package-summary.html#threading. The article you linked to dates from 1998 and is obsolete. – JB Nizet Jan 19 '12 at 16:16
  • My mistake: see also [link]http://stackoverflow.com/questions/491323/is-it-safe-to-construct-swing-awt-widgets-not-on-the-event-dispatch-thread/491377#491377 – Peter Jan 19 '12 at 16:42

1 Answers1

1

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

Peter
  • 123
  • 2
  • 9