tags:

views:

206

answers:

4

A shout out to the Swing gurus out there!!

I've been doing Swing programming for several years but have always been unclear on this.

As you know Swing/AWT gives you several ways to execute a particular action when a button is clicked. I've seen it done several different ways in the applications I've worked on. The project I'm currently working on tends to follow this approach:

someButton.setActionCommand("mycommand");
someButton.addActionListener(listener);

--snip--

public void actionPerformed(ActionEvent event) {
    String command = event.getActionCommand();
    if (command.equals("mycommand"))
       doThis();
    else if (command.equals("someothercommand"))
       doThat();
etc.

This seems kind of clunky to me - is there any benefit to this style of programming, or is it better to use Swing Actions?

Or are there different situations where the different approaches are better/worse?

+4  A: 

IMO, it is better to use separate listeners for Actions.

That way you leave the delegating of what action should happen up to Swing. You don't have to compare Strings to decide what to do.

Having one huge ActionListener for more than one action feels like breaking the pattern to me.

jjnguy
Additionally, it allows for the code in the action listener to be larger, or for the delegation to be internally held. I believe that the main reason to use the command element is if there are different commands that can cause actions out of the same component. However, off the top of my head I cannot think of one that is inherent to Swing/AWT.
aperkins
+2  A: 

From a design point of view, I think it is better to have one class to handle one specific action for a component as opposed to one class that is a "lets handle everything for all components here" type of design.

Also, if you use Action you can a) apply it to more than one component (e.g. a button and a menu item) b) call setEnable to enable/disable it for all components its attached to and c) Also use it to define various settings on the components its attached to (namely, the text label, the tooltip text, the accelerator key, icon, etc.). This last one is done via the putValue method and calling this method again will change the settings for all components its attached to.

Specifically, I would advise to subclass AbstractAction for your needs.

Avrom
A: 

its Useful if you have several buttons or components that perform the same action (ie. several exit buttons on the same page will use the same code)

Set them all to the same action command and they will all use the same code in the listener

JButton.addActionListener(this);
JButton2.addActionListener(this);
JButton.setActionCommand("exit");
JButton2.setActionCommand("exit");

public void ActionPerformed(ActionEvent e){
    if(e.getActionCommand=="exit")
        System.exit(0);
}
Brandon
No, if they perform the same action...give them the same action listener....no need for command strings
jjnguy
Ok thanks, still learning and this is what i was taught they were used for :)
Brandon
It can be useful...but it would be better to give the listener to both components.
jjnguy
A: 

I know that's demo code but since you're working on this stuff I thought I'd mention that swing tends to be really repetitive if you're not careful.

Using Action classes tends to let you refactor better. In swing, one of the best ways to start is to ensure that NO strings are in your code. Nearly every "New" will be in a loop of some sort, reading from a dataset (Often the dataset is as simple as an array)--Once you start reading from a dataset like that, actions can help you a lot.

You use data to create your action, data to create your control, and data to associate the two--in this way you can end up very close to (or at) 0 lines of code for a new control.

Once you start programming this way and can see the patterns, it's at least as quick as the repetitive way and much less error prone.

Bill K