• Post Reply Bookmark Topic Watch Topic
  • New Topic

simple addActionListener problem  RSS feed

 
Greenhorn
Posts: 19
Eclipse IDE Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I'm trying my hand at writing a little beginners program. It's a fortune telling program that presents a pseudo-randomly selected string from an array and presents it on a Jframe.

The problem I'm having is trying to add an action listener to the JButton button, but I don't seem to be able to invoke any action when the button is pressed. Before compiling, I get the error 'cannot use this in a static context'. button.addActionListener(this);

I've no idea what I'm doing wrong. Perhaps I don't need to run this fortune telling program to predict that I'll be poor if I try to make a living out of java programming

This is my code so far below:
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Michael Gordon, Welcome to the CodeRanch.

First, put your code with the tag code. It will be easier to help you.

Seconde, which line do you get that error?
 
Michael Gordon
Greenhorn
Posts: 19
Eclipse IDE Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hebert,

Sorry about that. I've enclosed the code now.

It's line 27 where the error appears. 'this' is where the error appears. Not sure if that's where the error occurs or whether my coding is lacking something.


Michael
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this refers to the current FortuneTeller instance. To add it as an ActionListener it must be an ActionListener. However, it only extends java.awt.Frame, and therefore it is not an ActionListener.

For that line to work you must do two things:
1) add implements ActionListener after extends Frame.
2) implement actionPerformed(ActionEvent e) as specified by the ActionListener interface.

A better solution is to use an (anonymous) inner class, but let's not take too big steps right now.
 
Michael Gordon
Greenhorn
Posts: 19
Eclipse IDE Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

Thanks for the prompt response. So I think, after reading your post, I understand a little more I've done wrong here.

so 'this' refers to the FortuneTeller instance and I needed to implement the ActionListner interface to allow that class to wear the ActionListener hat.

1) I've now added the ActionListener interface to the FortuneTeller class
2) I've now also implemented the abstract method actionPerformed.

I do still have an error tho, the button.addActionListener(this); The error tells me that I "cannot use this in a static context". I tried changing the class from static to non static, but that didn't seem to help.

I've attached my modified code just in case it helps elaborate any:


 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know, I completely missed the fact that this code is executed in the main method, not an instance method or constructor.

Step 1: Undo what I suggested. It's not needed.
Step 2: move the button.addActionListener(); line to after the ButtonHandler class (but before the random number generation.
Step 3: change that line to button.addActionListener(new ButtonHandler());

This will use an instance of the ButtonHandler class that you already had created and already implemented ActionListener as the ActionListener for the button.
 
Michael Gordon
Greenhorn
Posts: 19
Eclipse IDE Linux Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You Sir are a genius.

Many thanks for your help on this one. It works a treat now.

Perhaps I have structured my code in an irregular way. I'm still a newbie to programming and have a long way to go still.



Michael
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!