| Author |
Need help with addActionListener
|
Steve Vittoria
Ranch Hand
Joined: Jan 12, 2005
Posts: 33
|
|
Hi everyone, First off, please forgive me if I'm posting this in the wrong section. I'm new to Java and I'm having a bit of trouble setting up and action event. I have an error with the following... It's complaining about "previousButton.addActionListener(this);", saying that... "non-static variable this cannot be referenced from a static context" Can someone please explain to me what I'm doing wrong here?
|
 |
samir Sadiki
Ranch Hand
Joined: Apr 22, 2006
Posts: 31
|
|
Hi Steve Try removing the keyword static from the declaration of your method, as follows: public void addComponentsToPane(Container pane). I am realtively new to java myself, I hope a more experienced person will post a reply to your question.
|
 |
Steve Vittoria
Ranch Hand
Joined: Jan 12, 2005
Posts: 33
|
|
Hi samir! Thanks for replying! If I remove the static from the addComponentsToPane method, I then get two errors. 1st problem: I now get the following error with the line that has the addActionListener... "addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (movietheater.MovieTheater)" 2nd problem: I get a bunch of errors like the following... "non-static method addComponentsToPane(java.awt.Container) cannot be referenced from a static context" If I remove static from every method, then that resolves my second problem, but is this a good solution? Also I don't understand my first problem. I probably should have posted all of my code. Here it is... [ April 26, 2006: Message edited by: Steve Vittoria ]
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24081
|
|
Use static sparingly. "Real" programs use it very little. Removing the "static"s all over the place was a good thing. As far as the other error goes: it's not enough to define the actionPerformed() method -- the class this appears in must also explain that it's an action listener by declaring that it implements the ActionListener interface; i.e., public class MyClass extends JWhatever implements ActionListener {
|
[Jess in Action][AskingGoodQuestions]
|
 |
Steve Vittoria
Ranch Hand
Joined: Jan 12, 2005
Posts: 33
|
|
Thanks for the ActionListener tip, I think that solved that issue. However if I remove static from all the methods, than I get an error stating "No main classes found". If I put back static in the main method, I then get the same error message as before... "non-static method createAndShowGUI() cannot be referenced from a static context"
|
 |
samir Sadiki
Ranch Hand
Joined: Apr 22, 2006
Posts: 31
|
|
Hi Steve The main method should always be static as far as I know. It is better to create the GUI in the constructor, and take care of displaying it in the main. Try this:
|
 |
Steve Vittoria
Ranch Hand
Joined: Jan 12, 2005
Posts: 33
|
|
Works perfectly now! Thanks samir and Ernest! Now I'll have to take a closer look the code you provide and see what was wrong with me version. Thanks again guys!  [ April 26, 2006: Message edited by: Steve Vittoria ]
|
 |
samir Sadiki
Ranch Hand
Joined: Apr 22, 2006
Posts: 31
|
|
You are welcome Steve. Good luck!
|
 |
 |
|
|
subject: Need help with addActionListener
|
|
|