Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

action Handlers: Do they have to be static??

 
Peggy Gamous
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just got finished with a project for school. It is all finished so don't worry about helping to much...

Ok so I have a JButton and I add an action handler to it. This is ok I think:

theButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
TestProgram.oG_ButtonActionPerformed();
}
});

This is inside a constructor that makes this button and a panel for it. Now as it is it is static.

I had to change a bunch of variables to static to make it run.

My question is in two parts:

1) Is it ok if it is static and I had to change everything it needs to static? Does it matter? It ran the way I wanted...

2) And either way: How do I make it an instance method?

It confuses me because

1) how can an event be handled by a static method? (Oxymoronic comes to mind)

And

2) I only used 1 instance of this class at any time and It didn't seem to matter either way so I couldn't figure it out by testing different things and I was wondering what it would have happened if I had more than 1 instance of this class in my program???

Please don't send me to the swing forum, I need to better understand static vs instance, Thanks!

[ June 22, 2006: Message edited by: Peggy Gamous ]
[ June 22, 2006: Message edited by: Peggy Gamous ]
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peggy,
You had to make your method oG_ButtonActionPerformed() static because you're calling it from an anonymous inner class (new java.awt.event.ActionListener()
{...}).

Another alternative is to make your TestProgram class implement the ActionListener interface.

So you'd have something like:

 
Campbell Ritchie
Sheriff
Pie
Posts: 48943
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peggy: Welcome to the Ranch.

Disagree with Dave Wingate, I am afraid.

Don't make your class implement the ActionListener interface.

And don't make the oG_ButtonActionPerformed() method static.

To make the method an instance method: very easy. Don't write Write

And you don't haveTaLoveJAVA, you choose whether to do programming or not.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dave Wingate:
Peggy,
You had to make your method oG_ButtonActionPerformed() static because you're calling it from an anonymous inner class[/CODE]


No, an anonymous inner class can without any problems call instance methods of the outer class (as long as the anonymous class itself is defined inside an instance method).

And that's what I'd do.
 
Peggy Gamous
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help Dave:


Another alternative is to make your TestProgram class implement the ActionListener interface.

But it was still static after I did that.

Campbell I'm not conplaining about loving JAVA, it is just a question. I wanted to learn Web Design and it was like JAVA blind-sided me out of nowhere.

And don't make the oG_ButtonActionPerformed() method static.
[/

That Is the problem: I do want it to be an instance method but can't get it to...

I honestly don't think that will change anything...


Ilja, thatnks but I can't get it to work...
as long as the anonymous class itself is defined inside an instance method
this is what I am trying to do!!! But even though its in the class with the constructor and the action handler part, the inner anonymous class, is also within the constructor when I try to write code in the oG_ButtonActionPerformed() method it is still static...
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep. Thanks for pointing out my error. Indeed, annonymous inner class does have access to the instance methods of the encapsulating class.

Despite my above retardation, I still think it's elegant to let the main class implement ActionListener.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48943
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
elegant to let the main class implement ActionListener.

And have a dozen buttons and have this sort of code?
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm with Campbell Ritchie on this one. I much prefer to be able to say "Here is a JButton X and when it's clicked on, here's what happens" rather than "Here's a JButton X" and then "If somebody clicked on JButton X, then here's what happens" elsewhere in the code. That way I keep JButton X's code all in one place.

And in our design, I don't have to pretend that the main class IS-an ActionListener. In fact the main class HAS-an ActionListener, or more likely it HAS several ActionListeners that do different things. I'm preferring composition to inheritance in this case.
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fair enough. My preference is for code like Campbell's example; I prefer to keep all of my "action" code together in one place. When I maintain an old GUI, I don't like to hunt for the action code.

I don't disagree with your arguments, though. My preference does go against the usual is-a vs has-a methaphors, so you're right to tell me to jump in a lake.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, having things done in a standard way is also a good thing, even if that standard way doesn't measure up to the latest design fads.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like my action code to be one line that calls a controller.

Building GUI elements is one job.
Associating the active ones to actions is another.
Dispatching events (the handler) is another.
Doing the actual work is another.

I haven't done much Swing work, but I've used a little design several times. Here's the glue code and the handler:

I see one command that doesn't call the controller. Oops.
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, very nice. I'm a convert.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:


No, an anonymous inner class can without any problems call instance methods of the outer class (as long as the anonymous class itself is defined inside an instance method).

And that's what I'd do.


Correction, as long as it's defined in a non-static context. It could just as easily be a initialization block or instance variable initializer.
 
Peggy Gamous
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the input!

I got 100% on my project!!!



Yea!!!





Hey Stan, That some pretty cool stuff!
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<hr></blockquote>
I honestly don't think that will change anything...

[/qb]

No, these to method calls are NOT the same. The first one requires that the method is static method; if it is an instance method, then the compiler will complain. The second will call the method whether it is an instance method or a static method.

Layne
[ June 26, 2006: Message edited by: Layne Lund ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:

Correction, as long as it's defined in a non-static context. It could just as easily be a initialization block or instance variable initializer.


Right, thanks for the correction!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic