Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does Swing methods not run in static methods ?

 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5148
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic