Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Identifying GUI events

 
Paul Trow
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Igor,

Thanks - that was very helpful.

Paul
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Igor Stojanovic
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic