File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes action Handlers: Do they have to be static?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "action Handlers: Do they have to be static??" Watch "action Handlers: Do they have to be static??" New topic
Author

action Handlers: Do they have to be static??

Peggy Gamous
Greenhorn

Joined: Jun 22, 2006
Posts: 3
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 ]

why Do I HAV'TA to LoVe JavA /?
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
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:



Fun programming etcetera!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36475
    
  16
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Peggy Gamous
Greenhorn

Joined: Jun 22, 2006
Posts: 3
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

Joined: Mar 26, 2002
Posts: 262
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

Joined: Oct 13, 2005
Posts: 36475
    
  16
elegant to let the main class implement ActionListener.

And have a dozen buttons and have this sort of code?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

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

Joined: Mar 26, 2002
Posts: 262
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
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
Ah, very nice. I'm a convert.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Jun 22, 2006
Posts: 3
Thanks for all the input!

I got 100% on my project!!!



Yea!!!





Hey Stan, That some pretty cool stuff!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
<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 ]

Java API Documentation
The Java Tutorial
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: action Handlers: Do they have to be static??
 
Similar Threads
Something I just dont get about the OOP concept
Static method and non-static data
Help on Servlets- web.xml not working
Range class
This program is throwing back error