Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with addActionListener

 
Steve Vittoria
Ranch Hand
Posts: 35
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 {
 
Steve Vittoria
Ranch Hand
Posts: 35
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome Steve.
Good luck!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic