File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes GUI button errors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "GUI button errors" Watch "GUI button errors" New topic
Author

GUI button errors

Paul Baker
Greenhorn

Joined: Feb 05, 2002
Posts: 6
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

Joined: Mar 22, 2002
Posts: 7
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

Joined: Mar 26, 2002
Posts: 262
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 ]

Fun programming etcetera!
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
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
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
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!
 
wood burning stoves
 
subject: GUI button errors