Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

get action command  RSS feed

 
Dave Morley
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question:

I am enlivenning two buttons .
The code follows:

public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();

if (arg == "Go !"); // check to see if the go button has been pressed
{
startData = Integer.parseInt(startField.getText());// must parse it.
stopData = Integer.parseInt(stopField.getText());
stepData = Integer.parseInt(stepField.getText());

for(int i = 0; i <= 15; i++)
checkArray[i].setBackground(Color.MAGENTA);


}

if (arg == "Clear");
{
startField.setText("");
stopField.setText("");
stepField.setText("");
}


the question is this, the program compiles and runs except when you press the Go button the code for the clear button is also carried out. Why is this???

Any help is appreciated.

Regards
Dave Morley
 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if (arg == "Go !"); // check to see if the go button has been pressed
{


Remove the ";" after the if condition. Everything between the braces will be executed no mather what arg will contain.
 
Dave Morley
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Manuel

Will try it and let you know

Regards
Dave Morley
 
Dave Morley
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Manuel

It did work.

Regards
Dave Morley

 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, to be on the safe side, replace arg == "Go !" and arg == "Clear" with arg.equals("Go !") and arg.equals("Clear"). It is well-konown around the ranch that comparing two strings with == is like running with scissors; except that you are actually more likely to trip and fall when comparing the strings.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!