Event handling... inner classes or not? what do you recomend?

Me question is should you

use

JButton addbutton=new JButton("Add");

addbutton.addNewActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

...

}

});

or this

class Window implements ActionListener

public void actionPerformed(ActionEvent e)

{

if (e.getSource()==addbutton)

{

...

}

if (e.getSource()==...

}

So when I would have say menu and 15 menuitems to listen, I would prefer the innerclass system becauses it somehow produces cleaner code, but it creates pretty much of those $1 $2 files... will it be a problem?

[683 byte] By [mikaelkuisma] at [2007-9-26 1:22:20]
# 1
second one better if u r going to do many operations on many buttons and controls(checkbox,radio ,list....)first one is good and smart if you have one or two controls in your program..Right ?
balajilogsan at 2007-6-29 0:59:15 > top of Java-index,Archived Forums,Java Programming...
# 2

I prefer to use inner classes since it makes clean code. However I prefer to define one innerclass per source type and not the getSource() trick. This gives a clean separation of responsability. You can use the same innerclass for different sources if the reaction should be the same for different user inputs.

Filip

filipicom at 2007-6-29 0:59:15 > top of Java-index,Archived Forums,Java Programming...
# 3
hjkhkhjkhjkhjk
ismat1 at 2007-6-29 0:59:15 > top of Java-index,Archived Forums,Java Programming...
# 4

>I prefer to use inner classes since it makes clean code.

>However I prefer to define one innerclass per source

>type and not the getSource() trick. This gives a clean

>separation of responsability. You can use the same

>innerclass for different sources if the reaction should

>be the same for different user inputs.

you mean something like this:

Button b1=new Button("Edit1");

Button b2=new Button("Edit2");

b1.addActionListener(new InnerClass(b1));

b2.addActionListener(new InnerClass(b2));

...

mikaelkuisma at 2007-6-29 0:59:15 > top of Java-index,Archived Forums,Java Programming...
# 5

That's what I do, I have a separate inner class for each button. You could have one class that handled actions for several buttons, but then the class would contain lots of code like

if (e.getSource() == addButton)...

and one of the rules-of-thumb of object-oriented processing is that if you find switch-like constructs in your code, that's a hint that you should break it into separate classes... hence what I do. Just as filipicom said.

DrClap at 2007-6-29 0:59:15 > top of Java-index,Archived Forums,Java Programming...