This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Identifying GUI events 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 "Identifying GUI events" Watch "Identifying GUI events" New topic
Author

Identifying GUI events

Paul Trow
Greenhorn

Joined: Feb 20, 2005
Posts: 8
If I have two buttons in a GUI, how do I distinguish the two button-click events? If I have

button1 = new JButton("Button 1");
button1.addActionListener(what_goes_here1?)
button2 = new JButton("Button 2");
button1.addActionListener(what_goes_here2?)

do I need two of the following, one for each button? If so, how do I distinguish the two events?

public void actionPerformed(ActionEvent e)

Thanks.
Igor Stojanovic
Ranch Hand

Joined: Feb 18, 2005
Posts: 58
Hi Paul, first register ActionListener to your buttons like this:


and then in your actionperformed method write this:


I hope this helps you



kind regards
Igor

[ February 20, 2005: Message edited by: Igor Stojanovic ]
[ February 20, 2005: Message edited by: Igor Stojanovic ]
Paul Trow
Greenhorn

Joined: Feb 20, 2005
Posts: 8
Igor,

Thanks - that was very helpful.

Paul
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Unless the actions performed by the two buttons are similar, I typically create two different event handlers. I also don't like to come up with names for them, so I use anonymouse inner classes. While the syntax is a little complicated the first time you see it, it doesn't take long for it to becomes second nature:


HTH

Layne

p.s. For future reference, we have a GUI forum where such question are more appropriate. You should probably mosey over there if you have more questions along these lines.
[ February 22, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Igor Stojanovic
Ranch Hand

Joined: Feb 18, 2005
Posts: 58
Originally posted by Layne Lund:
I also don't like to come up with names for them, so I use anonymouse inner classes.


Thx for the TIP Layne. Anonymouse classes are much better option when dealing with events.
[ February 22, 2005: Message edited by: Igor Stojanovic ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I like a style where the event handlers are extremely small ... one line to call another method. If you get into the Model View Controller pattern that method might be on a controller object. Imagine the handlers for the File/Exit menu item and an Exit button. Each has one line that calls the same exitTheApplication() method.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Stan James:
I like a style where the event handlers are extremely small ... one line to call another method. If you get into the Model View Controller pattern that method might be on a controller object. Imagine the handlers for the File/Exit menu item and an Exit button. Each has one line that calls the same exitTheApplication() method.


I think this is one reason why Swing introduced the Action class. Actions provide a nice mechanism for sharing code between menus, buttons, and other GUI controls. In fact, the API is such that you only add a line of code to register an action with the control upon creation. You don't need any extra code (beside what is in the Action class itself) to handle the event.

Layne
[ February 24, 2005: Message edited by: Layne Lund ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Identifying GUI events