This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Troble working with JButtons.." Watch "Troble working with JButtons.." New topic
Author

Troble working with JButtons..

Scott Cajun
Greenhorn

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:
feed1.addActionListener(this);
feed2.addActionListener(this);
feed3.addActionListener(this);

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Troble working with JButtons..
 
Similar Threads
Very Very New
ScrollBar Problem
Please need help with calculator program...
Adding JButton Array to a applet issue
Opening a new window.