Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to handle events in java

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
To simplify, use arrays. For instance,
 
Sheriff
Posts: 22004
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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: 22004
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.
 
I just had the craziest dream. This tiny ad was in it.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic