• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

which is the best way for event handling

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 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
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ideally use the second approach but with MVC - i.e., let the action events be generated at the View and be passed to the controller to perform further actions(methods)
 
Garry Kalra
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you that controller sort of event handling.
Gaurav
reply
    Bookmark Topic Watch Topic
  • New Topic