• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Listening frames vs. listening buttons

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
today's feeble attempt to support the empire
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic