Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GUI button errors

 
Paul Baker
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey I'm attempting to add buttons to a GUI but I keep getting the following error.
addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (Life)
next.addActionListener(this);
Here is the code. I realize this is not the full set of code the actionPerformed is in a different class.

[ March 26, 2002: Message edited by: Cindy Glass ]
 
Adam Blinkinsop
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From your posted code, you are attempting to add an object of the Life class as an ActionListener to the buttons, where Life doesn't implement ActionListener.
If you're going to "next.addActionListener(this)", then you'll need to implement ActionListener in the Life class.
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another alternative to having class Life implement action listener is to add an annonymous class as the action listener.
Instead of:
next.addActionListener(this);
Try:
next.addActionListener(new ActionListener{
public void actionPerformed (ActionEvent ae) {
// stubb -- place event handeling code here
} // actionPerformed(ActionEvent)
} // annonymous class from ActionListener
); // end addActionListener method
[ March 26, 2002: Message edited by: Dave Winn ]
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
next.addActionListener(this);

You can't add "this" as an actionListener, since your class doesn't implement ActionListener

This class is implementing ActionListener so you may use it as an actionListener
Or the second solution is to use an anonymous inner class, but I don't like it much!
I hope it helps you!
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realize this is not the full set of code the actionPerformed is in a different class.

Does the definition of your class look like
... class ClassName implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String buttonPressed=e.getActionCommand();
if (buttonpressed=="next")
{ //Something to do when you press this button
}
if (buttonpressed=="quit")
{
//...
}
}
}
If yes you may do something like this

Of course, your class must be able to access the class implementing ActionListener!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic