• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Peter Den Haan Pls help regarding event handling

 
Garry Kalra
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am facing a difficult situation.
Suppose here is my class
class Test {
Test() {
JButton b = new JButton("Hi");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
.....
}
});
JButton h = new JButton("Hello");
h.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
.....
}
});
}
public static void main(String args[]) {
Test t = new Test();
}
}
The above code creates a large no. of class files as separate
ActionListener are created.
class Test implements ActionListener {
JButton b = new JButton("Hi");
JButton h = new JButton("Hello");
Test() {
b.addActionListener(this);
h.addActionListener(this);
}
public static void main(String args[]) {
Test t = new Test();
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b) {
...
}
else if (ae.getSource() == h) {
...
}
.....
}
}
The above code doesn't creates any extra class files.
Can anyone pls guide which approach is acceptable and is considered a
good.
Also there is a third approach in which i can create and inner class to hendle events.
Pls guide as to which will be more maintenable and better.
Also the following thing is bugging me :-
should i use the following:-
Because we are supposed to use swing components, what kind of event
handling should be provided. The above is AWT event handling which is
at the core. I also heard that Action interfaces can be implemented.
Any help will be greatly appreciated.
Gaurav Kalra
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Garry Kalra:
Can anyone pls guide which approach is acceptable and is considered a good.
Acceptable and good is whatever approach expresses the intent of the code most clearly and concisely. There is nothing problematic about producing lots of class files, if that makes the code clearer.
Making Test implement ActionListener will (in a real life situation) blur the responsibilities of Test. A class is most clear if it has a single, focused purpose. Also, using a single ActionListener (inner class or not) for multiple disparate GUI elements makes it necessary to stick conditional logic inside it. Conditional logic makes code harder to understand; often, polymorphism is preferable. This, too, points in the direction of separate ActionListeners.
Because we are supposed to use swing components, what kind of event handling should be provided. The above is AWT event handling which is at the core. I also heard that Action interfaces can be implemented.

Swing inherits its event model from AWT, so there's nothing wrong with that. You can usefully employ Actions especially if you provide a toolbar in addition to menu items.
- Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic