Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JButtion and Event Handle, tell me how to use it!! 2

Status
Not open for further replies.

qb828

Programmer
Sep 27, 2000
98
US
hello! how are you?
i am writing for the game called"Mancala". i have found a lot of source codes already. but they don't really help me much. this is how it looks like.

your boxes=> b3 b2 b1
your score box | 3 | 3 | 3 | my score boc
0 | 3 | 3 | 3 | 0
b1 b2 b3 <== my boxes

there are 3 stones in each boxes except score boxes which only get added on..bottom boxes are mine and top boxes are yours. you just pick box and add to next boxes and set that box as empty.

your score box | 3 | 3 | 3 | my score boc
0 | 3 | 3 | 3 | 0

???
My question is how to use ActionEvent for JButtons.
i need to set a button clicked to zero and add 1 to next buttons, upto however stones were there in the button clicked on. add counterclockwise.if i click on box2 on bottom.
so it will be like this:
your score box | 3 | 3 | 4 | my score boc
0 | 3 | 0 | 4 | 1

you set number on button with JTextfield. but how do you know which button was clicked on since they are all same buttons with same text on them ,this case, number.

please help me. you get &quot;ActionEvent e&quot; and you have to use text comparsion to find the button. on my case they are all same number on them..so how to find which button i push and do calculations....
thanks very much...
 
So for your JButtons you say something like:
btn_1 = new JButton(&quot;Pick&quot;);
btn_1.addActionListener(this);
// Here we are nominating your class as the listener
// So you need to add implements ActionListener


Then just define the actionPerformed method:

public void actionPerformed(ActionEvent e){
if(e.getSource()==btn_1){
// Perform some actions -call methods etc
}
else if(e.getSource() == btn_2){
// Some other stuff
}
// etc etc

}

I hope this is what you meant? Note - this is better than definiing an ActionListener for each button - especially if you have heaps of them.
b2 - benbiddington@surf4nix.com
 
thanks bangers!!
but i need to label the button with integers,
JButton b1= new JButton(&quot;3&quot;);
JButton b2= new JButton(&quot;3&quot;); for initially

and add integers, like add 1 to b1 and reset it as &quot;4&quot;
and add 1 to b2 and reset b2 as &quot;4&quot;
then can you do this with integers?
does e.getSource() know which button of &quot;3&quot; i clicked on?

since b1==&quot;3&quot; and b2=&quot;3&quot;, and e.getSource=&quot;3&quot;, how do you know which one?
can you do this?

public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
// Perform some actions -call methods etc
}
else if(e.getSource() == b2){
// Some other stuff
}
// etc etc

}


i am not good at explainning but i hope i com across my point to you. thanks again for your help.

Q
 
I have mailed you back - but getSource() returns the object,and the Label is available at b1.getLabel()


function incrementLabel(JButton button){
// convert String to Integer
int lbl_number = Integer.parseInt(button.getLabel());
// increment - add one to whatever it is
lbl_number ++;

// convert back to string and put label back
button.setLabel(Integer.toString(lbl_number));
}


I have not tested this, but you get the idea right? ;-)
b2 - benbiddington@surf4nix.com
 
e.getSource() returns the object that caused the actionPerformed method to execute. It does not return the value of the caption on the JButtons. So yes, it does know which JButton caused the method to execute.

Also (b1 != &quot;3&quot;) this would be done by (b1.getText().equals(&quot;3&quot;)) I hope this helped! ;-)
- Casey Winans
 
Banger offered a nice fix, but you don't want to use getLabel() or setLabel()... they are depreciated methods inherited from the AbstractButton class. I hope this helped! ;-)
- Casey Winans
 
thanks alot casey & bangers.
you guys helped me a lot.
for some reasons, Button b1 = new Button(&quot;start&quot;);
i thought b1 was equal to &quot;start&quot;. i thought that b1 was string.......&quot;start&quot; was just label....
anyway
now i see it clear..thanks again

Q

i posted another Question... for radiobutton.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top