views:

207

answers:

5

Hi, I am writing a small Java app (on Windows, hence the _on_vista appended to my name).

I got 3 buttons, all of which will react to a click event, but do different things.

Is the following code the accepted way or is there a cleaner way I do not know about? On one half, it works, on the other half, something doesn't seem right...

Thanks

cool_button_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        coolfunction1();
    }
});

cool_button_2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        coolfunction2();
    }
});

// etc ...

The functions that get called will spawn off threads as needed, so on and so forth.

UPDATE - Both were good (pretty much the same) answers. I accepted the one with the lower rep to share the wealth. Thanks again guys.

+3  A: 

Yes, this is the correct way to do this. It's a bit clumsy (to have to write five long lines of code just to be able to call a method) but that's Java :(

Aaron Digulla
haha, after about a year of php, working on this project in java is an absolute delight! a real language api is something i took for granted.
You don’t write five lines for calling a method. You write five lines for calling a method when a button is pressed. That is a difference.
Bombe
I think Aaron's objection was that some languages would just let you pass a function pointer to coolfunction1, rather than having to wrap it with a new object.
Outlaw Programmer
Then why doesn’t he say so? Why do I have to keep guessing what people really mean when they are clearly unable to express themselves? :(
Bombe
@Bombe: I assume that people read my reply in context.
Aaron Digulla
lol ... seriously though, i think i love java
A: 

I'd say that code is fine, as Java code goes. It's a good example for Java's often-criticized verbosity - in this case due to the lack of closures.

Michael Borgwardt
I havent used closures because i didnt understand them, but your comment is starting to connect some dots for me, thanks
Of course, local classes are (rather minimal) closures.
Tom Hawtin - tackline
Basically, a closure is a standalone method (without a class) that you can pass as parameter to a method - exactly what is needed in your example. The anonymous classes you're using offer the same functionality, just with a lot of boilerplate around very little code.
Michael Borgwardt
+1  A: 

That’s not too bad. I prefer to use Actions and create JButtons from them:

Action fooAction = new AbstractAction() { ... };
JButton fooButton = new JButton(fooAction);
Bombe
Thanks for the input, I will keep this in mind should I run into a similar problem
Yeah, You can reuse action with menus and disable them.
l_39217_l
+1  A: 

Its one of the correct ways to do it.

You are using anonymous listeners but there is an alternatif

nicely explained on http://java.sun.com/docs/books/tutorial/uiswing/events/intro.html

where you write a class that implements ActionListener and have it handle the logic for you

Peter
A: 

I tend to do something like this:

public class ButtonPanel implements ActionListener {
    JButton button1, button2, button3;
    public ButtonPanel() {
        button1 = new JButton(this);
        button2 = new JButton(this);
        button3 = new JButton(this):
        ...
    }

    public void ActionPerformed(ActionEvent e) {
        if( e.getSource() == button1 ) action1();
        else if( e.getSouce() == button2  ) action2();
        else if( e.getSource() == button3 ) action3();
    }
}
JoshuaD
One problem I have with this is now actionPerformed is now part of this class's public API, even though no client code can use it directly. If you insist on using a single ActionListener, it's probably better to create a new, private instance and pass that to your controls.
Outlaw Programmer
It's a question of priorities; whether a clean API or easily read code is more important to your set of constraints. I don't think my suggestion is something that's good *most of the time* but it is certainly another valid way to do things. I'm a little disappointed you voted it down. Ah well.
JoshuaD