How to register array of button and identify in actionPerfomed method.
Code :
import java.awt.*;
import java.awt.event.*;
class Bt implements ActionListener{
Frame f;
Button b[]=new Button[3];
Button b1[]=new Button[3];
Button b2[]=new Button[3];
TextField tf;
Bt(String s){
Frame f=new Frame(s);
TextField tf=new TextField();
for(int i=0;i<b.length;i++){
b[i]=new Button();
b[i].setLabel(String.valueOf(i));
b[i].setBounds((60*i),250,30,30);
//b[i].addActionListener(this);
f.add(b[i]);
}
//for(int d=0;d<b.length;d++){
//b[d].addActionListener(this);
//}
tf.setBounds(20,60,100,100);
f.add(tf);
f.setLayout(null);
f.setSize(400,400);
f.setVisible(true);
}
public void actionPerformed(ActionEvent k){
/*for(int i=0;i<b.length;i++){
if(k.getSource()==b[i]){
switch(i){
case 0:tf.setText("Welcome"); //how can i check which button is generated event.
case 1:tf.setText("Let's play");
case 2:tf.setText("Quit");
//break;
}
}
}*/
}
public static void main(String... arg){
new Bt("ButtonType");
}
}
how can i check which button is generated event? I want if Button 1 is pressed then it show Welcome in texfield, if button 2 then it show let's play. how can i achieve it?
Re: How to register array of button and identify in actionPerfomed method.
How do you identify "Button 1"? I see there are 3 arrays of Buttons.
Look at the methods for the Button class and see if there are any methods that can be used to save an identity for the button. For example the label
If you were to use the JButton class, then there is the ClientProperty methods that you could use to save and retrieve id info for the buttons.