my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes How to handle events in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to handle events in java" Watch "How to handle events in java" New topic
Author

How to handle events in java

Juan Gilberto
Greenhorn

Joined: Aug 31, 2009
Posts: 4
como evitar el uso de if cuando estoy manejando muchos botones en un formulario

public void actionPerformed(ActionEvent e){

if(e.getSource()==btn1){
btn1.setText("X");

}

else if(e.getSource()==btn2){

btn2.setText("Y");

}
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> como evitar el uso de if cuando estoy manejando muchos botones en un formulario

if you could translate this to english, a lot more people could possibly help you
Juan Gilberto
Greenhorn

Joined: Aug 31, 2009
Posts: 4
Michael Dunn wrote:> como evitar el uso de if cuando estoy manejando muchos botones en un formulario

if you could translate this to english, a lot more people could possibly help you



sorry no speak english . . .traduction google . . please wait . .i´m java student in institute

and avoid using if the event is generated when you press a button I'm making a game with 9 buttons for a year and I'm using if. . but leave many

public void actionPerformed(ActionEvent e){

if(e.getSource()==btn1){

btn1.setText("X");

}

else if(e.getSource()==btn2){

btn2.setText("X");

}


else if(e.getSource()==btn3){

btn3.setText("X");

}


else if(e.getSource()==btn4){

btn4.setText("X");

}


else if(e.getSource()==btn5){

btn5.setText("X");

}


else if(e.getSource()==btn6){

btn6.setText("X");

}



Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Juan Gilberto wrote:
Michael Dunn wrote:> como evitar el uso de if cuando estoy manejando muchos botones en un formulario

if you could translate this to english, a lot more people could possibly help you



sorry no speak english . . .traduction google . . please wait . .i´m java student in institute

and avoid using if the event is generated when you press a button I'm making a game with 9 buttons for a year and I'm using if. . but leave many

public void actionPerformed(ActionEvent e){

if(e.getSource()==btn1){

btn1.setText("X");

}

else if(e.getSource()==btn2){

btn2.setText("X");

}


else if(e.getSource()==btn3){

btn3.setText("X");

}


else if(e.getSource()==btn4){

btn4.setText("X");

}


else if(e.getSource()==btn5){

btn5.setText("X");

}


else if(e.getSource()==btn6){

btn6.setText("X");

}






This will work




esto funciona ( this works ---> google translate)


or if I understand your second post...

Object o = e.getSource();
((JButton) o).setText("x");

sets any button clicked to "x";

Same as Michael Dunn.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
still not sure of the problem, but his will eliminate all your if's

Juan Gilberto
Greenhorn

Joined: Aug 31, 2009
Posts: 4
Michael Dunn wrote:still not sure of the problem, but his will eliminate all your if's




This method is generally applicable to all nine buttons or copy and paste q there several times. . The idea is to simplify algo parecido a esto . .

if person select a button
put an x or y to the button. . but when a button is in x, the other should be in and. . is to make a game Triqui
Juan Gilberto
Greenhorn

Joined: Aug 31, 2009
Posts: 4
Michael Dunn wrote:still not sure of the problem, but his will eliminate all your if's




This method is generally applicable to all nine buttons or copy and paste q there several times. . The idea is to simplify
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
To simplify, use arrays. For instance,
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Can someone provide a translation of the subject? I can then change it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Iban Cardona
Greenhorn

Joined: Apr 15, 2009
Posts: 9
Rob Prime wrote:Can someone provide a translation of the subject? I can then change it.


Subject: How to handle events in java
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Thank you very much.
 
GeeCON Prague 2014
 
subject: How to handle events in java