This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Listening frames vs. listening buttons

 
Simone Cittadini
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm used to write listeners in this way :

JButton button = new JButton("I'm a button!");
button.addActionListener(new InnerClassWhichImplementsActionListener())

That's because when I started programming in java I used jbuilder and it creates code in that way when you draw the GUI.

I've seen some examples using this style :

public class MyFrame extends JFrame implements ActionListener {

public void actionPerformed(ActionEvent e){
switch actions based on e.getSource() ...

It seems to me a better way, less lines of code, class attributes are directly visible.

Since I'm still quite a newbie, before I start rewriting my old code, there's something I'm missing in the "Inner class method" making it preferable ?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To me, having the JFrame being the listener typically makes it *harder* to understand the code. It also makes it harder to refactor the class, for example splitting it into several smaller classes.

The only way to make it worse is to use action commands...

So, as I can't fully eliminate the possibility that I will have to maintain your code in some future days, please continue using inner classes...
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:
So, as I can't fully eliminate the possibility that I will have to maintain your code in some future days, please continue using inner classes...


Keeping in mind that code that can possibly be used by more than 1 action should be externalized outside of the inner class.

I only use frame level listeners for this relevent to the frame itself, like WindowListener or MouseListener/MouseMotionListener on Containers.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only use frame level listeners for this relevent to the frame itself, like WindowListener or MouseListener/MouseMotionListener on Containers.
Cannot agree more.
I seem to give instructions about how to write an ActionListener at regular intervals, most recently here.

Are you using a Listener which applies specifically to the JFrame or other Component? If you are, then make the Component implement MouseMotionListener and use its addMouseMotionListener(this) method.

Are you using the same Listener more than once, either to do the same thing in different places (eg an exitButton and an exitMenuItem, both of which have the same listener), or similar things (eg the examples beginners are taught where the redButton turns something red and the blueButton turns something blue)? In that case you need a concrete named class to do it. Call it ExitListener, ColourListener, or whatever. You can either use a public class (in which case you can reuse it for other applications) or a private inner class for the Listener.
For colours, pass the Color object to the Listener constructor.

For a Listener you are using once and once only, use an anonymous inner class. No two ways about it.
As Ilja Preuss says, see how inelegant it is to have a method in your JFrame which reads like this:Even though you will see examples like that in books

CR
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic