problem: open a frame in a thread

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kalar
    New Member
    • Aug 2007
    • 82

    problem: open a frame in a thread

    I want to have a thread and in this thread i want to make a JFrame window
    i try this but i think it doesn't work correct:
    Code:
    public class AboutWindow  implements Runnable
    {
            Thread hello ;
            public AboutWindow()
            {
            	hello = new Thread();
            	hello.start();
    
            }
        
        public static void main(String[] args)
        {
        	AboutWindow hd = new AboutWindow();
        	
        }
    
            public void run( )
            {
            JFrame tes = new JFrame();
            tes.setVisible(true);
        	tes.setSize(400,330); //300,200
        	tes.setLocation(450,200);
        	Container contentPane = tes.getContentPane();
        	tes.setLayout(new BorderLayout());
        	tes.setResizable(false);
        	tes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	contentPane.setBackground(Color.CYAN);
        	
        	JLabel th = new JLabel("heloo");
        	contentPane.add(th,BorderLayout.CENTER);   
            } 
    }
    It only opens a JFrame without the label tha i add on lines:29-30 and also i can't close it from x in the window.( it only dissapear the window, but back it still running).Where is the problem?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Your AboutWindow is a Runnable that should be passed the Thread constructor.
    The Thread will run it when it is started itself (read the API documentation for
    the Thread class). So your constructor should look like this:

    [code=java]
    public AboutWindow() {
    hello = new Thread(this);
    hello.start();
    }
    [/code]

    kind regards,

    Jos

    Comment

    • kalar
      New Member
      • Aug 2007
      • 82

      #3
      Thank you veru much!!! it works

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Opening a window in a non-EDT is a bad idea. Please study this:

        This Swing Java Tutorial describes developing graphical user interfaces (GUIs) for applications and applets using Swing components

        Comment

        Working...