Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Listening frames vs. listening buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Listening frames vs. listening buttons" Watch "Listening frames vs. listening buttons" New topic
Author

Listening frames vs. listening buttons

Simone Cittadini
Greenhorn

Joined: Apr 28, 2006
Posts: 13
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

Joined: Jul 11, 2001
Posts: 14112
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...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39076
    
  23
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
 
GeeCON Prague 2014
 
subject: Listening frames vs. listening buttons