Yuri
Yuri

Reputation: 39

Swing Event Thread Java

In this code HelloWorldApp simply extends JFrame . Why dont i need to put the 'app' object definition in the invokeLater method ? And why 'app' need to be final ? Thanks

final HelloWorldApp app = new HelloWorldApp();
SwingUtilities.invokeLater( new Runnable() 
{
    public void run() 
    {
        app.createAndShowGUI();
    }
});

Upvotes: 0

Views: 96

Answers (1)

Stefan
Stefan

Reputation: 444

  1. All changes to the UI state of swing components has to be done within the EDT (Event Dispatch Thread). Therefore the definition of the 'app' can be done in the main thread as long as in the constructor HelloWorldApp() doesnt create UI components or changes the state of UI components.

  2. I see no reason for specifying the class as final. In the latest tutorial it is not final any longer. See Hello World tutorial

Upvotes: 2

Related Questions