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

Action Adapter

Greg Davis
Greenhorn

Joined: May 02, 2006
Posts: 3
hi
Is a good idea to use ationadapter for a jbutton in oreder to performed an action, or is another simple and shorter way?
thanks in advance for any help
Greg
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38340
    
  23
This sort of question comes up all the time.
Beware of Adapters. Apart from the fact they have peciluar spelling, you have to be very careful with the method names. One capital letter out of place, and you end up with a Listener whihc doesn't do anything.

I am assuming that you have got all our import statements in place already.

My rule of thumb, is that what you do for Listeners depends on how many Listeners you want.
  • Do you want a Listener which actually applies to the GUI itself? For example, one which shows the position of the mouse on screen, or follows the mouse. Then make the appropriate Component implement an interface, eg MouseMotionListener, and use addMouseMotionListener(this).
  • Have you got several Components which will implement the same, or similar, Listeners. In which case you will want a class for that Listener.
  • Have you got one Component whihc does one action, which is substantially different from everything else? In that case you put the Listener directly inside the Component as the anonymous inner class.

  • Adapters: An Adapter is an abstract class, and there is an Adapter class which corresponds to every Listener interface with TWO OR MORE methods. So there isn't an ActionAdapter, because the ActionListener interface only has one method. But you can always make your own ActionAdapter if you so wish.

    Example one: You have a DisplayPanel called panel, and you want to display the mouse position on a JTextField on the DisplayPanel. If you don't use a MouseMotionAdapter, you get the DisplayPanel to implement the MouseMotionListener interface, which has two methods.As an alternative to addMouseMotionListener(this), you can have
    ORIf you use an Adapter, you only override the methods you want to, and leave the other methods blank.

    If you have several Components you want to add Listeners to which are the same or similar, you are probably better off writing a class to implement their methods. Look at this example where you want to change colours with different JButtons:-You can have your ColorListener class as a private class within the body of the class with the GUI in, or as an ordinary class outside it. You can have the same with a MouseMotionListener or MouseListener or whatever, but those two Listeners have 2 and 5 methods respectively whihc have to be implemented. Remember you can leave some method bodies empty.
    Or you can have a class which extends one of the Adapter classes, in which case you don't need to override all its methods.

    The 3rd and I think most elegant method to set an ACtionListener is to set it as an anonymous inner class. You use this where you have one Listener which does something substantially different from all other Listeners. So you might have an exitButton, which closes the app altogether, and no other closing method.

    The way I do it is to go into Eclipse, set up half the method, and wait for the IDE to give me an error message . But the way you ought to do it is in stages like this:
    1: The method call
    2: Put your cursor in the () and push enter twice.

    3: put the name of the Listener, and new and () in the gap:

    4: Convert that code to to class body with {} and new lines:

    5: Put the method signature in the class body:

    This is the first stage at which the compiler will let it compile, even though it will do absolutely nothing with that empty method.
    6: Final stage: Fill in the methdo body.

    Voila!

    Just count my spelling errors.

    If whatever you are putting into the actionPerformed() method takes more than about two lines, you would do better to move it into another method and call it there.

    I hope that is of use and hasn't confused you. There should be lots about Listeners in the Java tutorial, and it should tell you whihc sort of component takes whihc sort of Listener.

    CR

    [ May 03, 2006: Message edited by: Campbell Ritchie ]
    [ May 03, 2006: Message edited by: Campbell Ritchie ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Action Adapter