asiavoices
Programmer
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("<your button text>"
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 "ADD":
recordCounter ++;
setBackground(Color.red);
break;
case "DELETE":
recordCounter --;
setBackground(Color.blue);
break;
case "ADD 2":
recordCounter += 2;
setBackground(Color.black);
break;
case "L O G O U T":
System.exit(0);
break;
default:
setBackground(Color.yellow);
System.out.println("Error in Button selection"
break;
} // End Switch
System.out.println("Counter hit is: " + recordCounter );
}
else
{
System.out.println("Error in Button selection"
}
} // end of actionPerformed() method
Any ideas?
Thanks,
Christopher
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("<your button text>"
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 "ADD":
recordCounter ++;
setBackground(Color.red);
break;
case "DELETE":
recordCounter --;
setBackground(Color.blue);
break;
case "ADD 2":
recordCounter += 2;
setBackground(Color.black);
break;
case "L O G O U T":
System.exit(0);
break;
default:
setBackground(Color.yellow);
System.out.println("Error in Button selection"
break;
} // End Switch
System.out.println("Counter hit is: " + recordCounter );
}
else
{
System.out.println("Error in Button selection"
}
} // end of actionPerformed() method
Any ideas?
Thanks,
Christopher