Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2005

Tip: Looking for answers? Try searching our database.

Actions - Best practice

Thread view: 
chrilar@gmail.com - 07 Oct 2005 09:59 GMT
Just thought I'd get a consensus of how other developers handle actions
and what they prefer.

As far as I see it there are many methods to handle actions on e.g. a
JButton.
What are the preferences of this newsgroup?

Thanks for feedback

Sincerely Chris

Examples:

#1  classic, but crude when multiple buttons or actions.

myClass implements ActionListener{
  JButton button1
  JButton button2
  ...

  button.addActionListener(this);

  public void actionPerformed(ActionEvent e){
         if(e.getSource()==something){
            perform task
         } else if(......

  }
}

#2 more modern, My favorite mainly for the ability to reuse Actions for
different components.

myClass {
  MyActionClass action1 = new MyActionClass("button title"){
    public void actionPerformed(ActionEvent e){
       perform some action
    }
  }
  JButton button = new JButton(action1);
}
myActionClass extends AbstractAction{
  public void actionPerformed(ActionEvent e){}
}
Pete Barrett - 07 Oct 2005 19:17 GMT
>Just thought I'd get a consensus of how other developers handle actions
>and what they prefer.
>
>As far as I see it there are many methods to handle actions on e.g. a
>JButton.
>What are the preferences of this newsgroup?

I don't know about 'this newsgroup', but as far as I can see there are
two separate questions here.

1) which class implements the Listener? possibilities being:
    a) the class which creates the button (your example 1).
    b) an anonymous inner class (your example 2).
    c) a private (or protected) named inner class.
    d) a dedicated Listener class.

2) How many ActionS should a Listener service? possibilities being:
    a) all of them
    b) a related group of them
    c) one of them

If there aren't too many, I tend to put them all in the same Listener
where I can find them, and if the class won't be sub-classed, then it
may as well be the class itself, so 1a2a.

I never use anonymous inner classes (I think they break up the flow of
the code and make it difficult to see the logic), but I quite often
use combinations of 1c and d with 2b and c, depending upon
circumstances.

Pete Barrett


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.