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)
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 ]
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
Joined: Jun 22, 2006
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...
Joined: Mar 26, 2002
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.
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.
Joined: Mar 26, 2002
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.
<hr></blockquote> I honestly don't think that will change anything...
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.