aspose file tools*
The moose likes Beginning Java and the fly likes Why does Swing methods not run in static methods ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why does Swing methods not run in static methods ? " Watch "Why does Swing methods not run in static methods ? " New topic
Author

Why does Swing methods not run in static methods ?

WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85
I read that Swing methods will not run in static methods and one solution is to create an instance of the application’s frame class in the static main method .

I have two queries.

Why don't Swing methods work in a static method ?

Why creating an instance of the application's frame class help ?

Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
Swing methods are intended to run on an object. If you have two JButtons, how would a static method know which button has been pressed? So, as is normal with object design, most members of Swing components are instance members.
WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85
Campbell Ritchie wrote:Swing methods are intended to run on an object. If you have two JButtons, how would a static method know which button has been pressed? So, as is normal with object design, most members of Swing components are instance members.


Oh good point. Thanks alot.

On a second note,



GraphUI2 object can be created without declaring beforehand because the object is created in the same class itself ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
What you are doing is invoking a constructor. Somewhere in that constructor it say something like this:-or, if it extends JFrame like this:-…only without the spelling errors. So you have (in that constructor) other code which starts the application off. You create an instance of GraphUI2 and it either creates a frame in its constructor, or it is a frame; until that frame is closed and disposed of, it will run a thread and remain visible.

By the way: the correct Swing way to create an app is by starting the EDT thread: details here.
WeiJie Lim
Ranch Hand

Joined: Sep 05, 2012
Posts: 85
Understood both of your examples.

But I am a little confused over this.
is the creation of an instance of a class. And

the constructor



contains all the details like setSize etc.

Am I right ?

Creating the instance of a class in this manner is not needed in this context right ?

I have a previous exercise in which I created instances of a class in this manner, that's why I am confused with the code also creates a class as well, but without variables ?

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4540
    
    5

The new operator followed by a class constructor call evaluates to a reference to an instance of that class. As with any expression, the value can be assigned to a variable if needed.

It doesn't have to be assigned to a variable. In this case, it is simply discarded, since all code of interest is in that constructor. In other cases, the value may be used as a parameter to a method, as in:Here too the 'new JLabel' isn't assigned to a variable reference.


luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38756
    
  23
You are right that you are creating a new object. Darryl has already explained that.
The constructor might contain setSize, etc., or it might call another method (maybe setUpGUI()) which contains a setSize() call.

We can see your knowledge growing
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does Swing methods not run in static methods ?