Question on Multiple Listeners implementation
I've been wondering about this. Lets say you have a GUI designed that has a whole bunch of GUI Fields, such as a whole bunch of Buttons or a GUI which listens for property changes on a bunch of different objects. Is it better to use anonymous inner classes to listen on each object or just have the GUI implement the Listening interface and handle it accordingly?
Example:
Bunch of inner classes:
Code:
public void initConnections() {
getButton1().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button1Clicked();
}
});
getButton2().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button2Clicked();
}
});
getButton3().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button3Clicked();
}
});
getButton4().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button4Clicked();
}
});
getButton5().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button5Clicked();
}
});
getButton6().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button6Clicked();
}
});
getButton7().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button7Clicked();
}
});
getButton8().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button8Clicked();
}
});
getButton9().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button9Clicked();
}
});
getButton10().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button10Clicked();
}
});
}
Or a GUI which implements the ActionListener
Code:
public void initConnections() {
getButton1().addActionListener(this);
getButton2().addActionListener(this);
getButton3().addActionListener(this);
getButton4().addActionListener(this);
getButton5().addActionListener(this);
getButton6().addActionListener(this);
getButton7().addActionListener(this);
getButton8().addActionListener(this);
getButton9().addActionListener(this);
getButton10().addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == getButton1()) {
button1Clicked();
} else if (e.getSource() == getButton2()) {
button2Clicked();
} else if (e.getSource() == getButton3()) {
button3Clicked();
} else if (e.getSource() == getButton4()) {
button4Clicked();
} else if (e.getSource() == getButton5()) {
button5Clicked();
} else if (e.getSource() == getButton6()) {
button6Clicked();
} else if (e.getSource() == getButton7()) {
button7Clicked();
} else if (e.getSource() == getButton8()) {
button8Clicked();
} else if (e.getSource() == getButton9()) {
button9Clicked();
} else if (e.getSource() == getButton10()) {
button10Clicked();
}
}
Is this just down to personal taste? Or is there a valid reason to select one over the other such as better performance or uses less space?