java - ActionListener and ActionEvent best practise query - Stack Overflow
java - ActionListener and ActionEvent best practise query - Stack Overflow
Asked 8 years, 10 months ago Modified 8 years, 10 months ago Viewed 111 times
within my code I currently have all my ActionListener's as Anonymous inner classes like so:
0 item.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//code here
}
}
now this feels fine for me when the code is no more than 20ish lines however it feels clunky
for more than 20 lines. So, I could implement ActionListener and have a method
ActionPerformed(ActionEvent ae)
{
if ae.getSource() == item
{
// code here too
}
}
but I have no intention of re-using this code anywhere else... I could create another class to
handle this as well but that doesnt really help as I still wish to use variables that were
instantiated within my parent class.
I feel that this would look best as I can hide the events at the bottom and focus on the bulk of
the running project. Is this possible to achieve?
Share Improve this question Follow asked Mar 26, 2015 at 17:35
penfold1992
315 3 14
Why not use a non-anonymous inner class? – RealSkeptic Mar 26, 2015 at 17:39
Could you please explain this? I am not familiar with a non-anonymous inner class – penfold1992 Mar
27, 2015 at 10:01
A basic inner class, with a name, defined as a member of the class. – RealSkeptic Mar 27, 2015 at 10:30
Share Improve this answer Follow answered Mar 26, 2015 at 17:38
Kevin Workman
41.8k 9 68 107
Unfortunately... I am using Java 6. lambdas are indeed far easier and less cluttered but unfortunately I
do not have the correct Java version to utilize them :( – penfold1992 Mar 27, 2015 at 10:01
@penfold1992 Java 6 reached its end of life in February of 2013. Using it is a pretty big security risk, so
you better have a very good reason for using it! – Kevin Workman Mar 27, 2015 at 14:11
well i have a good enough reason... I work for a very large company of over 50,000 people and I do not
have any power to convince the company that it would be necessary to update to Java 8. its just what I
have to work with :( – penfold1992 Mar 28, 2015 at 23:48
If all the buttons run the same code, you can define a (named) inner class that implements
ActionListener and runs the code, or you can even make your enclosing class an
0 ActionListener and add it as the action listener for all the buttons, so that its
actionPerformed() runs the code for all.
If, on the other hand, each button requires a different action, you can have the different
actions as methods like you requestod. I'd suggest passing the event to them in case you
need event information:
etc.
Then you create the anonymous class, but the only thing its actionPerformed() does is call
the appropriate method.
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnActionOK(e);
}
});
backButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnActionBack(e);
}
});
forwardButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnActionForward(e);
}
});
An inner class can call a method of the enclosing class. This way, you keep the anonymous
classes short, and you can put all the long code in its own methods in the enclosing class.
Share Improve this answer Follow answered Mar 27, 2015 at 10:46
RealSkeptic
34.2k 7 54 80