aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Anonymous Classes question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Anonymous Classes question" Watch "Anonymous Classes question" New topic
Author

Anonymous Classes question

Brandt Charles
Ranch Hand

Joined: Apr 17, 2006
Posts: 57

I'm taking an Intro to Software Engineering class using Java and we just covered GUI's. This is the first I had heard of Anonymous Classes so I thought I'd post my question here. Anyway, the text mentions Anonymous Classes should only be used if the event handling code is only very small such as one line. Is this a hard and fast rule? GUI programming appears to be a bit cumbersome (see my sig), so if the code is clean, why couldn't one use anonymous classes other than under this condition as mentioned by the text?

Furthermore, what would be the good rule of thumb on using Adapter Classes versus the regular, longer way of programming event handlers?

I appreciate the help and resource. It's never fun to post programming questions on angry forums such as those on Anandtech as the inevitable accusations of free homework help arise.


Green, green, green!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

You are hardly the "greenest" if you are talking anonymous classes.

Originally posted by Brandt Charles:
. . . Anonymous Classes should only be used if the event handling code is only very small such as one line. Is this a hard and fast rule?

There are few "hard and fast" rules. My standard has always been that code "works and can be seen to work".
GUI construction code in Java can get pretty ugly. Adding a bunch of anonymous event handlers into the mix doesn't help. By making the anonymous class one line (usually a call to some method which contains the details of handling the event) you can keep your construction work seperate from the various run-time events. That makes your code easier to read and maintain, and is therefore a good thing.


It's never fun to post programming questions on angry forums such as those on Anandtech as the inevitable accusations of free homework help arise.


We try to keep it friendly around here.


[How To Ask Questions On JavaRanch]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
I am sure I have written about anonymous inner classes as ActionListeners before. Of course, other Listeners, as well as Runnables can also be anonymous.

I use the rule of thumb that if you have more than one Listener which do the same thing, or something very similar, use a named class, and pass the differences in the constructor; the classic example given in beginners' exercises is buttons which change their colour; set up this sort of thing:-

I don't like the format which is found in some books; it might be simple to learn, but if you have more than one button, you can end up with so many if elses and else ifs, that the actionPerformed method looks a right mess. I only use the (this) technique when it specifically refers back to your Component (eg add a MouseMotionListener to a panel which displays the mouse co-ordinates on a text field).

The anonymous inner class is useful when you have one component with a unique action. An example using an exitButton:- [But if you have an exitButton and an exitMenuItem, don't sue anonymous classes, use an ExitListener class, otherwise you are duplicating things.]

Stage 2: put two new lines in the ()
Stage 3: fill in the gap.
Stage 4: ActionListener is an interface; make it into a class body with {}.
Stage 5: ActionListener must have one method, with the signature as below. It would be more complicated if we used .addMouseListener() because MouseListener has to have five methods
At this point (and not before) you can persuade the compiler to accept it, but nothing will happen until stage 6: Fill in your method body.
And as Joe Ess has told you, putting in a call to another method rather than lots of coding will make it simpler to see what is going on.
I hope that is of some use to you
CR
Brandt Charles
Ranch Hand

Joined: Apr 17, 2006
Posts: 57

Thanks for the replies, they were helpful. It is nice to see someone with more experience than me not fond of the (this) way of coding the listeners. Unfortunately, some books held in high esteem teach that as the primary way of doing it. The text used for my class teaches the long way, then also using Adapter and Anonymous classes.
Rakesh Chaudhary
Ranch Hand

Joined: Feb 19, 2008
Posts: 120
Thanks for giving such a awesome explanation..it wouldn't have been more clearer than this......

Thanks and keep living on the EDGE......
Minh Nam
Ranch Hand

Joined: Sep 10, 2011
Posts: 57
Thanks guys for the useful discussion. It is valuable for me although it was 5 years ago


Advanced java topics
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi,

I was refered to this older post so i wanted to add a comment/ question to it. I tried this on something i am working on now instead of using an inner Class as i usually had. I had an ActionCommand set for the button but
as far as i can tell after commenting it out, i no longer need an ActionCommand as when the button is clicked it goes straight away to the "actionPerformed(ActionEvent e)" method.
Is this correct , or are there cases where i will still need to set one using this type of ActionListener set-up?


Thanks
Mike
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1096
    
  10

With reference to your other post, when you set the ActionCommand, instead of calling e.getSource().equals(), you can use e.getActionCommand().equals("SaveAction")...

the former compares objects and the later Strings...so, if you compare using e.getSource(), setting the action command makes no difference to your program. And all this is needed if you have only one ActionListener for your entire class.

As pointed out in the other post, that is not a recommended way of writing ActionListeners.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

And if you use an action listener for only one component (as is the case with anonymous listeners) then no, you don't need the action command. I hardly ever set one.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi,

Thanks to both of you for your comments! I like this style actually, and i will probably appreciate it more with a larger more complex program. Although i think this will take up allot more space then the way i am learning now
if there are a bunch of buttons and so on. But possibly much easier to read and comprehend.

Thanks
Mike
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Button: an object.
Connects to a Listener: an object.
These two objects relate to each other, and one implements the behaviour the other offers.
One button and one listener. It may means there are more classes, and more objects, but those classes are small and easy to read. Remember memory is cheap nowadays; I spent some time this morning creating 8800000 Integer objects, accompanied by 8800000 Map.Entry objects which nobody noticed.

Many buttons, one Listener. An impenetrable mess of if-elses, difficult to update if you change the button. Lots of comparisons for equality before you find the required block.
Somewhere on the Ranch there is an example from about 3 years ago. Somebody created a calculator, with an enormous actionPerformed() method, full of things like
It wasn't exactly like that, but similar. I haven't found it recently. I didn't have the heart to tell the rancher how much of her time they had wasted writing that method.

Just think how much easier it would have been withNow you can set up the buttons in a loop, using the loop index to initialise the buttons and the listeners simultaneously.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous Classes question