0

In the following code, why is the "My Application" window the window at the front? The main method constructs this window first, right? So shouldn't it be at the back when the "My Window" JFrame is produced?

public class MyApp extends JFrame {
public MyApp() { 
super();
setSize(300,600); 
setTitle("My Application"); 
setVisible(true);
}
public static void main(String[] args) { 
MyApp application = new MyApp(); 
JFrame window = new JFrame(); 
window.setSize(600,300); 
window.setTitle("My Window"); 
window.setVisible(true);
} 
}
mark
  • 2,841
  • 4
  • 25
  • 23

2 Answers2

3

This is simply undefined behavior; there's no guarantee that either window will always be in front. When I ran your code (Mac OS X, Java 6), "My Window" came up in front, but I have no doubt you're correctly describing what happens on your system.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
1

1) you can't move one JFrame toFront() over another JFrame

2) don't create more than one JFrame, there are another issues with this Top-Level Container as toFront, toBack

3) you have look at JDialog or JWindow as another Window

  • with parent to the JFrame

  • with setModal if required

  • with ModalityTypes is required

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • @Tharwen [did you read this code there are two instances of JFrames](http://stackoverflow.com/a/9554657/714968) 1) first `MyApp extends JFrame`, 2) second `JFrame window = new JFrame();` 3) both are visible out of `InitialThread` – mKorbel May 09 '12 at 13:22
  • I didn't mean that it was wrong! Just that it doesn't specifically answer the question: "why is the "My Application" window the window at the front?" – Tharwen May 10 '12 at 14:29