File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Troble working with JButtons.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Troble working with JButtons.." Watch "Troble working with JButtons.." New topic

Troble working with JButtons..

Scott Cajun

Joined: Dec 11, 2006
Posts: 3
Baisical its a marine simulation, and i am coing a GUI where there are 3 buttons to feed 3 different fish, bt i cannot seem to get the right code layout to get it to work, can you try and see what the problem is?

it compiles.. but the button doesn't do anything
also, if i try to add:

it gives me a compile error... "addActionListener(java.awt.Event.ActionListener) in javax.swing.AbstactButton cannot be applied to (InterfaceFrame)"
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> it compiles.. but the button doesn't do anything

the button/s need a listener before they can do anything.

addActionListener(this) needs the class to implement the ActionListener interface
i.e. class InterfaceFrame extends Frame implements ActionListener
and then your code goes into actionPerformed(..), but this style of using
ActionListener is not the best.

Either create a separate class that is an ActionListener
e.g. class FeedListener implements ActionListener
or use anonymous inner classes that have a specific task
e.g. feed1.addActionListener(new ActionListener(){...

this part of your code may cause you painting problems
class InterfaceFrame extends Frame
JButton feed1 = new JButton("Feed Fish 1");

Frame is an AWT component, and JButton is a Swing Component.
Mixing the two is not recommended
I agree. Here's the link:
subject: Troble working with JButtons..
It's not a secret anymore!