Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Register Event Listener to Button? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Register Event Listener to Button?" Watch "Register Event Listener to Button?" New topic
Author

Register Event Listener to Button?

Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
Hi, I'm trying to hook up Button 'a' to an Event Listener. The program compiles, but when I click on button a nothing happens, and my Dos prompt prints ActionPerformed then hangs. I know it's long but any help at all will be appreciated. Thanks
import java.awt.*;
import java.awt.event.*;
class MyButton extends WindowAdapter implements ActionListener {
Frame f = new Frame("Sulapahoe Tribe");
Button a, b, c, d, e, g, j;
MyButton() {
f.addWindowListener(this);
f.setLayout(new FlowLayout());
f.setSize(500,300);
f.setBackground(new Color(102, 0, 102));
Button a = new Button("BinTallOne");
//here I try to add listener object to a
a.addActionListener(this);
a.setBackground(new Color(0, 204, 0));
a.setForeground(new Color(255, 255, 0));
Button b = new Button("Chloe");
b.setBackground(new Color(255, 153, 51));
b.setForeground(new Color(255, 0, 51));
Button c = new Button("Patsy");
c.setBackground(new Color(0, 0, 153));
c.setForeground(new Color(255, 255, 0));
Button d = new Button("OneEye");
d.setBackground(new Color(0, 0, 255));
d.setForeground(new Color(204, 255, 102));
Button e = new Button("Runamok");
e.setBackground(new Color(153, 51, 255));
e.setForeground(new Color(51, 153, 255));
Button g = new Button("MissKarenLea");
g.setBackground(new Color(153, 0, 51));
g.setForeground(new Color(204, 204, 0));
Button h = new Button("Bunny");
h.setBackground(new Color(0, 153, 0));
h.setForeground(new Color(255, 204, 0));
Button j = new Button("MeanJoe");
j.setBackground(new Color(255, 0, 204));
j.setForeground(new Color(0, 51, 0));
f.add(a);
f.add(b);
f.add(c);
f.add(d);
f.add(e);
f.add(g);
f.add(h);
f.add(j);
f.setVisible(true);

}

public void windowClosing(WindowEvent e) {
f.setVisible(false);
System.exit(0);
}

public void actionPerformed(ActionEvent ae) {
System.out.println("Action Performed");
}

public static void main(String args[]) {
MyButton mf = new MyButton();
}
}
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

I know this is going to sound like a lame response, but your program behavior sounds like exactly what you programmed.
When you bring the AWT subsystem into program code, a daemon thread gets brought up in the background that keeps the whole program running while it waits for more events to handle. Pressing the button once doesn't complete the code; you're not setting up a one-time thing.
Take another look at the code here and see if you agree.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
Hi Michael, I didn't put the moderator's name together until I saw your link. Although I've read your book (throughly)twice, I'm very green, with no programming exp whatsoever. It's an honor to get instruction from you, but it went over my head . I'm reading Chapt 10, create class that implements ActionListener, Construct component, Construct an instance of the class, Call addActionListener on component. I figured I'd hook up the other buttons when I get the mechanics down. "Not a one time thing?" Please assume I'm a moron.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Hey Mike -
No problem, but let's change out "moron" for "eager but sorely uninformed." The word moron makes me think of a hunchback with one eyebrow who gets real angry if he doesn't get a meat sandwich for lunch. I can't teach to that picture. ;-)
Your code is working, as programmed. Promise. It just may be that you expected something different. What your code does is set up a GUI with a bunch of buttons. With no other code, all these buttons will do is whatever you tell them. And all you've said so far is that you want to print something out to the command window when the Button object 'a' gets pressed.
By registering 'a' to the class, you're saying the MyButton object should listen whenever 'a' is pressed, and do whatever it likes: that is the code you have in your actionPerformed method.
Try this: put a System.exit(0); statement in your actionPerformed method. Compile and run this new version; you'll see the whole program exiting the first time you push that button.
The windowClosing() method is the same thing, except it relates to the action of dismissing a program through its window container.
To make it more interesting, you might add the MyButton object (also known as 'this' in your constructor) as a listener to each and every button. Then you'll see that each time one is pressed, the "response" code in actionPerformed() runs.
Does this help at all?
Coming at this with no experience, there's an awful lot to understand behind the scenes, even with this seemingly simple code example. Our book really isn't intended for people who are just getting started, but if you're getting value from it, great.
I'd recommend Just Java for starting out. Peter van der Linden is an *awesome* writer and has a simple, clear style. I recommend his book highly.
Good luck! and what else can we help you with?
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
Thank-you so much! Yes, I think that helps, let me digest.
Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
You're absolutley right my program works perfectly, only it's not what I wanted. I want seperate functions for the buttons displayed in the frame. Do I need to write a listener class for each button? or can I just keep overloading actionPerformed()?
Thanks Mike
PS I'd like each button to display a picture.
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

There's two ways to go about it. You could create a listener for each button -- an approach I prefer, but it's a bit tedious if you aren't a tenny bit clever about. The other approach is to write one or a couple of listeners. If one, then it catches all event sources (buttons). But sometimes a few can work very nicely.
Consider a GUI for a four-function calculator. You could divide the event work into one listener for the number buttons, one for the arithmetic operators, etc. When the NumberListener's actionPerformed() receives an event, it then must determine which button was pressed, but it knows it will be a number button and so can make assumptions that reduces the total code needed.
In your case, there seems to be no need for a complex strategy. Unless your buttons change on the fly, all you need is one actionPerformed() method that determines which button was pushed then does the right thing. Here's an example of that:

ActionEvent's getActionCommand() returns the event source's "action command". For a Button, that's just the button's label, unless you use setActionCommand() on the event source side to change it to something else.
This isn't great object-oriented style, but it's good enough to get you started. I can show you a more complex but more flexible approach when you're ready.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
Mike Kelly
Ranch Hand

Joined: Jul 18, 2001
Posts: 78
Thank-you Michael, I'll work on it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Register Event Listener to Button?