constructing actionPerformed method without using if statements

Hi,

I'm currently writing a basic swing program, and have used 'if' statements within the actionPerformed methods. I wondered if there was a better, neater way of doing this without using the 'if' statements.

My code so far is as follows,

method 1:

staticprivateclass IncrementDecrementActionimplements ActionListener{

boolean oddNumberClick =true;

publicvoid actionPerformed(ActionEvent e){

if (oddNumberClick= !oddNumberClick)

SimpleSwing.label.setText(labelPrefix +"Decrement");

else

SimpleSwing.label.setText(labelPrefix +"Increment");

}

}

method 2:

staticprivateclass ClickActionimplements ActionListener{

publicint numClicks = 0;

publicvoid actionPerformed(ActionEvent e){

if (SimpleSwing.label.getText().equals(labelPrefix +"Increment"))

numClicks++;

else

numClicks--;

SimpleSwing.label2.setText(labelPrefix2 + numClicks);

}

}

Any ideas or advice appreciated.

Thanks

[1997 byte] By [hirohonmaa] at [2007-10-2 10:15:55]
# 1
If you have one button that does two functions then you need an if statement.Create two buttons one for "increment" and one for "decrement" then you don't have to worry about the if statement.
camickra at 2007-7-13 1:40:47 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi,

Thanks for the reply. I wanted to keep the layout the same, with two buttons and two labels.

I was sure there was some way listed in the JButton API, which allows you to dynamically add and remove listeners but I can't remember or find it anymore.

Any advice or help appreciated.

Thanks.

hirohonmaa at 2007-7-13 1:40:47 > top of Java-index,Desktop,Core GUI APIs...
# 3
Yes the methods are addActionListenerremoveActionListener
tjacobs01a at 2007-7-13 1:40:47 > top of Java-index,Desktop,Core GUI APIs...
# 4

Hi,

I'm already using addActionListener to assign the ActionPerformed methods to their specific buttons. What I'm trying to do is rewrite the above ActionPerformed methods without specifically using 'if' statements.

Anybody got any ideas how I might be able to achieve this?

Any help appreciated.

Thanks

hirohonmaa at 2007-7-13 1:40:47 > top of Java-index,Desktop,Core GUI APIs...
# 5
> API, which allows you to dynamically add and remove> listeners but I can't remember or find it anymore. This was the question I answered.I'm not quite sure what you're idea is here but it seems to me like you're barking down the wrong tree
tjacobs01a at 2007-7-13 1:40:47 > top of Java-index,Desktop,Core GUI APIs...
# 6

> What I'm trying to do is rewrite the above ActionPerformed methods without specifically using 'if' statements.

What part of:

If you have one button that does two functions then you need an if statement.

did you not understand.

Spend you time on learning something usefull.

camickra at 2007-7-13 1:40:47 > top of Java-index,Desktop,Core GUI APIs...
# 7

I think there's a way, but it is very similar to your code:

public void actionPerformed(ActionEvent e) {

numClicks = SimpleSwing.label.getText().equals(labelPrefix + "Increment")?numClicks + 1: numClicks - 1;

SimpleSwing.label2.setText(labelPrefix2 + numClicks);

}

But keep in mind this: each time a button is pressed it will always call actionPerformed of all its listeners, so there is no way to call different methods (unless you use different listeners like Mouse listeners...).

328941a at 2007-7-13 1:40:47 > top of Java-index,Desktop,Core GUI APIs...
# 8

HI,

Thanks for the reply. That's exactly what I was trying to achieve. I have a similar solution for the actionPerformed method of the first button, using a boolean value for the clicks. The problem was that I just couldn't work out how to implement the same type of method with 'int numclicks'.

Thank you so much for your help.

Have a great weekend.

hirohonmaa at 2007-7-13 1:40:47 > top of Java-index,Desktop,Core GUI APIs...
# 9
And how is this different?<cond> ? <exp1> : <exp2>is equivalent toif (<cond>) <exp1> else <exp2>
dwga at 2007-7-13 1:40:47 > top of Java-index,Desktop,Core GUI APIs...