Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

use CASE for actionListeners? Possible? 2

Status
Not open for further replies.

asiavoices

Programmer
Sep 20, 2001
90
CA
Hello all,

It has dawned on me that why can't I use a case statement to check which buttons have been clicked? I think this is easier to figure out than a lot of "else ifs" branches.

So, instead of using the "normal way" I tried this but it gave me an "incompatible type" during compilation.

So instead of ...

e.getActionCommand().equals(&quot;<your button text>&quot;)

I tried printing out the results and it was the same as the button! Its just when I put it in the CASE statement that I got the error..

Here's my code .........

/* ==========================
= code here =
========================== */


public void actionPerformed(ActionEvent theEvent)
{
if ( theEvent.getSource() instanceof JButton)
{
String arg = theEvent.getActionCommand();

// System.out.println(arg);

switch(arg)

{
case &quot;ADD&quot;:
recordCounter ++;
setBackground(Color.red);
break;

case &quot;DELETE&quot;:
recordCounter --;
setBackground(Color.blue);
break;

case &quot;ADD 2&quot;:
recordCounter += 2;
setBackground(Color.black);
break;

case &quot;L O G O U T&quot;:
System.exit(0);
break;

default:
setBackground(Color.yellow);
System.out.println(&quot;Error in Button selection&quot;);
break;
} // End Switch


System.out.println(&quot;Counter hit is: &quot; + recordCounter );

}
else
{
System.out.println(&quot;Error in Button selection&quot;);
}
} // end of actionPerformed() method



Any ideas?

Thanks,

Christopher

 
This type of switch (or case) is not possible in Java. A switch only works int values (or things that can implicitly cast to int like char or byte). This a the biggest problem with switch statements in Java and makes them almost useless, not that you should be using switch statements much anyways...

If you decide to go forward with this then it will need to use nested if else statements.
 
Hi Wushutwist,

Guess what? I was trying to learn some string stuff and did come up with a way to do a CASE version of it..

So I'd like to share it with you and everyone ;-)


Here's what I have done...


public void actionPerformed(ActionEvent theButtonEvent)
{
if ( theButtonEvent.getSource() instanceof JButton)
{
String arg = theButtonEvent.getActionCommand();

switch (arg.charAt(0))

{
case 'A':
recordCounter ++;
setBackground(Color.red);
showButtonClicked(1);
break;

case 'S':
recordCounter += 2;
setBackground(Color.blue);
showButtonClicked(2);
break;

case 'P':
recordCounter += 4;
setBackground(Color.black);
showButtonClicked(3);
break;

case 'L':
System.exit(0);
break;

default:
setBackground(Color.yellow);
System.out.println(&quot;Error in Button selection&quot;);
break;
} // End Switch


System.out.println(&quot;Counter hit is: &quot; + recordCounter );

}
else
{
System.out.println(&quot;Error in Button selection&quot;);
}
} // end of actionPerformed() method


So what I'm basically doing is to take the first letter of the label of the button and use it a case argument :)

It seems to work...

cheers,

Christopher


 
Actually, this is just a hack and in the long run these types of work arounds cause more problems than they solve. What happens when you wish to add more than 1 button starting with the same letter? You'll need to do another work-around.
Then when someone else needs to maintain your code a few years down the line they will be left wondering what the heck you were trying to do.
Stick with the nested if statements, it is a much better solution in this instance and it is self-documenting.
 
Hi,

I don't understand why you want to use switch-statement ;) It doesn't fit very well for this kind of situations... and wushutwist is right, that kind of 'hack-solutions' are hard to maintain later.

But, however, here is one possible way to do it using switch statement:

public class test extends Frame implements ActionListener
{
public test()
{
Button add = ...
Button dele = ...
Button logout = ...
....
add.setActionCommand(Integer.toString(ADD));
dele.setActionCommand(Integer.toString(DELETE));
logout.setActionCommand(Integer.toString(LOGOUT));
}

public void actionPerformed(ActionEvent theEvent)
{
int cmd = Integer.parseInt(theEvent.getActionCommand());

switch(cmd)
{
case ADD:
...
break;
case DELETE:
...
break;
case LOGOUT:
...
break;
}
}

private static final int ADD = 1;
private static final int DELETE = 2;
private static final int LOGOUT = 0;
}

That example is little bit clearer for someone else who may have to read your code later. And it is also much easier to add new buttons, the range of the integer is sufficient enough ;)

-Vepo
 

Hello Wushutwist & Vepo,

Both of you are correct. I came up with this as nothing more than an exercise learning Java ;-)

Vepo, your suggestion was a good one too. I like yours better.

Thank you both,

Christopher

P.s. I still haven't solved my other problem.. ... the one about retrieving form input values and getting an error message...

thread269-145317
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top