• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to handle events in java

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");

}
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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");

}



 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
still not sure of the problem, but his will eliminate all your if's

 
Juan Gilberto
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To simplify, use arrays. For instance,
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone provide a translation of the subject? I can then change it.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic