Strange Java behaviour

Pieter de Goeje pieter at degoeje.nl
Thu Sep 20 21:30:40 PDT 2007


On Friday 21 September 2007, Aryeh Friedman wrote:
> On every JDK (linux-sun-jdk14,jdk15,diablo-jdk15,linux-sun-jdk16 and
> jdk16) I have tried this on it opens the JFrame then just dies
> (immediatly):
>
> import javax.swing.JFrame;
>
> public class Main
> {
>         public static void main(String[] args)
>         {
>                 JFrame frame=new JFrame();
>
>                 frame.pack();
>                 frame.setVisible(true);
>
>                 while(true)
>                         ;
>         }
> }
>
> I am using FreeBSD 7-Current with xorg 7.3 (gnome)
Your code is wrong. You cannot do GUI creation / updating outside the 
Swing/AWT event dispatching thread. Also, the while(true); is unnessecary 
(and a waste of CPU time) because java does not terminate while there are 
active threads.

For more information: 
http://java.sun.com/docs/books/tutorial/uiswing/concurrency/

Example:

import javax.swing.*;
public class Main {
  public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        JFrame f = new JFrame("Hello");
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setVisible(true);
      }
    });
  }
}

Regards,
Pieter de Goeje


More information about the freebsd-questions mailing list