Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Need help with addActionListener

 
Ranch Hand
Posts: 37
IntelliJ IDE Eclipse IDE Java
  • 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?
 
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: 37
IntelliJ IDE Eclipse IDE Java
  • 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 ]
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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: 37
IntelliJ IDE Eclipse IDE Java
  • 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: 37
IntelliJ IDE Eclipse IDE Java
  • 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!
 
Of course, I found a very beautiful couch. Definitely. And this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic