Hello to all,
I am creating one java applet in which i have to use many JButtons.
So i used an array of J Button. Now my program is able to create buttons but it is not able to use ActionListener or ActionCommand....
I am sending a part of code with this thread. Please let me know where I am wrong....
private void DrawElectric_field(Graphics g22, int i){
int m=1;
c= 3*Math.pow(10, 8);
freq= f1* Math.pow(10,9);
do{
fmin = m*c/(2*d1*Math.pow(10,-3));
m++;
}while(freq>=fmin);
temode = m;
g22.drawString("No.of Modes:" +temode, 800, 200);
for(int k=1, j = 0;k<=temode;k++,j++){
msg="TE"+k;
//JPanel fieldPanel = new JPanel(new GridLayout(1,2));
//JButton k1;
repaint();
JButton[] buttons= new JButton[temode];
buttons[j]=new JButton(msg);
getContentPane().add(buttons[j]);
buttons[j].setBounds(50+(100*k),600,80,40);
setLayout(null);//(new GridBagLayout());
buttons[j].addActionListener(wave.this);
buttons[j].setActionCommand(msg);
}
}
public void actionPerformed(ActionEvent ae) {
// TODO Auto-generated method stub
try{
if(ae.getActionCommand().equals("TE1")){
cnt=4;
}
if(ae.getActionCommand().equals("TE2")){
cnt=5;
}
// Here the same is applied for all the buttons i will create but now i am writing it just for two buttons TE1 and TE2
}
}catch(Exception e){ e.printStackTrace();}
}