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();}
			}

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();}
			}

You need :
jbutton.addActionListener(new ActionListener(){ public void actionperformed{ .....}
);
So can try with this.

You need :
jbutton.addActionListener(new ActionListener(){ public void actionperformed{ .....}
);
So can try with this.

hi
Thanks for your reply. but i m here again to trouble you more...
Please check am I doing it correctly......
because it is giving me error as "Syntax error, insert ";" to complete ClassBodyDeclarations" Please help me ... Its very urgent for me
Thanking you...

for(int k=1, j = 0;k<=temode;k++,j++){
	    	  msg="TE"+k;
	    	 //JPanel fieldPanel = new JPanel(new GridLayout(1,2));
	    	 //JButton k1;
	    	 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].addActionListener(new ActionListener(){ public void actionperformed{
	    		 cnt=3+k;
	    	 }}
	    	 );
	    	 
	    	 
	    	 buttons[j].setActionCommand(msg);
	    	 
		 }

hi
Thanks for your reply. but i m here again to trouble you more...
Please check am I doing it correctly......
because it is giving me error as "Syntax error, insert ";" to complete ClassBodyDeclarations" Please help me ... Its very urgent for me
Thanking you...

for(int k=1, j = 0;k<=temode;k++,j++){
	    	  msg="TE"+k;
	    	 //JPanel fieldPanel = new JPanel(new GridLayout(1,2));
	    	 //JButton k1;
	    	 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].addActionListener(new ActionListener(){ public void actionperformed{
	    		 cnt=3+k;
	    	 }}
	    	 );
	    	 
	    	 
	    	 buttons[j].setActionCommand(msg);
	    	 
		 }

Line no 11 should be

buttons[j].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){

Line no 11 should be

buttons[j].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){

Thanks for your help but still the buttons are not working. Now i am creating each button separately (removed array) still buttons are not working.
my code is too large to post. what do i do?
Need help.

thanking you.

public void first_te(Graphics g22, int i){
		
		JButton te1 = new JButton("TE1");
		getContentPane().add(te1);
		setLayout(null);
		te1.setBounds(250,600,80,40);
		te1.addActionListener(wave.this);
		te1.setActionCommand("TE1");
		System.out.print("first_te");
	}

public void actionPerformed(ActionEvent e){
    		 try{
    				
 				if(e.getActionCommand().equals("TE1")){
 					System.out.print("first_te");
 					cnt=4;	
 			 	
    	 }
    		 }catch(Exception ae){ ae.printStackTrace();}
    	 }

		    	 }
    	 );

my applwt contains two panels
1. "getContentPane()" panel
2. fieldPanel(which contains label and textfield) with grid layout.

Thanks for your help but still the buttons are not working. Now i am creating each button separately (removed array) still buttons are not working.
my code is too large to post. what do i do?
Need help.

thanking you.

public void first_te(Graphics g22, int i){
		
		JButton te1 = new JButton("TE1");
		getContentPane().add(te1);
		setLayout(null);
		te1.setBounds(250,600,80,40);
		te1.addActionListener(wave.this);
		te1.setActionCommand("TE1");
		System.out.print("first_te");
	}

public void actionPerformed(ActionEvent e){
    		 try{
    				
 				if(e.getActionCommand().equals("TE1")){
 					System.out.print("first_te");
 					cnt=4;	
 			 	
    	 }
    		 }catch(Exception ae){ ae.printStackTrace();}
    	 }

		    	 }
    	 );

my applwt contains two panels
1. "getContentPane()" panel
2. fieldPanel(which contains label and textfield) with grid layout.

If you want you can attach your code here.
Secondly just wondering what is this wave? You are adding wave.this to the actionListener

thank....wave is my class name(in applet)

import java.awt.*;
import javax.swing.*;

import java.awt.event.*;

import javax.swing.border.EmptyBorder;




public class [B]wave[/B] extends JApplet implements ActionListener {
	
	JTextField D1,F1;
	JButton submit,TE,TM,TEM,buttons[],te1,te2,te3,te4,te5 ;
	int d1,cnt,temode=0,k;
	float theta;
	double f1, inci_ang,fmin,fc,c,freq;
	//JButton[] buttons= new JButton[temode];
	String msg;

	public  void init() {
		 
	  	super.init(); 
	  	
	  	JLabel D1p = new JLabel("Distance:",Label.RIGHT);
	  	JLabel Fp = new JLabel("Frequency in GHz");
	  	JLabel F1p = new JLabel("Frequency:",Label.RIGHT);
	  	
	  		  		  	
	  	D1= new JTextField();
	  	F1= new JTextField();
	  	
	  	submit=new JButton("SUBMIT");
	  	TE=new JButton("TEm");
	  	TM=new JButton("TMm");
	  	TEM=new JButton("TEM");
	  	 
	  	 
	  	
		
	  	 final  JPanel customPanel = new JPanel() {
	 		   Dimension size = new Dimension(1000, 1000);
			    GradientPaint backgradient = new GradientPaint(0, 0, Color.cyan, 0, 800, Color.darkGray);
	 		  
	 		  public void paintComponent(Graphics g) {		        
	 			
	 			Graphics2D g2 = (Graphics2D) g;
	 	
	 		    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	 		    g2.setPaint(backgradient);
	 		    g2.fillRect(25, 25, 1000,1000);
	 		    g2.setPaint(backgradient); 
	 		    g2.setColor(Color.blue);
	 		    Font font1 = new Font("Arial", Font.BOLD, 16);
	 		    g2.setFont(font1);
	 		    int xpoints1[]= {200,500,650,650,500,200,200,350,650,500};
	 		    int ypoints1[]= {250,250,150,157,257,257,250,150,150,250};
	 		    int num1 = 10;
	 		    g2.fillPolygon(xpoints1,ypoints1,num1);
	 		    g2.setColor(Color.darkGray);
	 		    g2.drawPolygon(xpoints1,ypoints1,num1);
	 		    
	 		    DrawSecond_plate(g,1);
	 		    if(cnt==1){
	 		    	DrawElectric_field(g,1);
	 		    }
	 		    if(cnt==4){
	 		    	for_te1(g,1);
	 		    }
	 		   if(cnt==5){
	 		    	for_te2(g,1);
	 		    }
	 		  if(cnt==6){
	 		    	for_te3(g,1);
	 		    }
	 		   
	 		    int xpoints3[]= {200,500,650,650,500,200,200};
	 		    int ypoints3[]= {258,258,158,150+d1,250+d1,250+d1,258};
	 		    int num3 = 7;
	 		    g2.setColor(Color.lightGray);
	 		   
	 		    g2.fillPolygon(xpoints3,ypoints3,num3);
	 		    
	 		  }    
	  	  };


	  	  JPanel fieldPanel = new JPanel(new GridLayout(0,1,2,2));
	  	  JPanel constrainPanel = new JPanel( new BorderLayout() );
	  	
	
	
	   	    	
	  	  JPanel mainGUI = new JPanel(new BorderLayout(3,3));
	  	  mainGUI.setBorder(new EmptyBorder(3,3,3,3));

	  	  fieldPanel.add(D1p);
	  	  fieldPanel.add(D1);
	  	  fieldPanel.add(F1p);
	  	  fieldPanel.add(F1);
	  	  fieldPanel.add(Fp);
	  	  fieldPanel.add(submit);
	  	  fieldPanel.add(TE);
	  	  fieldPanel.add(TM);
	  	  fieldPanel.add(TEM);
  
	  	  // compact the fields to the top of the panel
	  	  constrainPanel.add( fieldPanel, BorderLayout.NORTH );

	  	  mainGUI.add(constrainPanel, BorderLayout.WEST);


	  	  //Register to receive action event
	  
	  	  submit.addActionListener(this);
	  	  submit.setActionCommand("submit");
	  	  TE.addActionListener(this);
	  	  TE.setActionCommand("TEm");
	  	  TM.addActionListener(this);
	  	  TM.setActionCommand("TMm");
	  	  TEM.addActionListener(this);
	  	  TEM.setActionCommand("TEM");
  
	  	  mainGUI.add(new JScrollPane(customPanel), BorderLayout.CENTER);
	  	  setContentPane(mainGUI);
	  	  validate();


	} 
	
	
	private void DrawSecond_plate(Graphics g12, int i) {
		
		int xpoints2[]= {200,500,650,650,500,200};
		int ypoints2[]= {(250+d1),(250+d1),(150+d1),(157+d1),(257+d1),(257+d1)};
		int num2 = 6;
		g12.setColor(Color.blue);
		g12.fillPolygon(xpoints2,ypoints2,num2);
		g12.setColor(Color.darkGray);
		g12.drawPolygon(xpoints2,ypoints2,num2);

		g12.setColor(Color.blue);
		g12.drawLine(200, 250+d1, 350, 150+d1);
		System.out.println("plates are drawn");
	}
	
	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;
		fc=fmin;
		g22.drawString("No.of Modes:" +temode, 800, 200);
		
		// Creating JButtons.......
		
		if(m==1){
			first_te(g22,1);
		}
		if(m==2){
			first_te(g22,1);
			second_te(g22,1);
		}
		if(m==3){
			first_te(g22,1);
			second_te(g22,1);
			third_te(g22,1);
		}
	
	}
	
	public void first_te(Graphics g22, int i){
		
		JButton te1 = new JButton("TE1");
		getContentPane().add(te1);
		setLayout(null);
		te1.setBounds(150,600,80,40);
		te1.setActionCommand("TE1");
		te1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){
    		 try{
    				
 				if(e.getActionCommand().equals("TE1")){
 					System.out.print("first_te");
 					cnt=4;	
 			 	
    	 }
    		 }catch(Exception ae){ ae.printStackTrace();}
    	 }

		    	 }
    	 );
		
		
		}
	
	public void second_te(Graphics g22, int i){
		
		JButton te2 = new JButton("TE2");
		getContentPane().add(te2);
		setLayout(null);
		te2.setBounds(250,600,80,40);
		te2.addActionListener(this);
		te2.setActionCommand("TE2");
		System.out.print("second_te");
	}
	
	public void third_te(Graphics g22, int i){
		
		JButton te3 = new JButton("TE3");
		getContentPane().add(te3);
		setLayout(null);
		te3.setBounds(350,600,80,40);
		te3.addActionListener(this);
		te3.setActionCommand("TE3");
	}
	
	public void for_te1(Graphics g22, int i){
		g22.setColor(Color.orange);
		g22.fillRect(200,257,(int) (300*(fmin/fc)),d1/6);
		g22.setColor(Color.red);
		g22.fillRect(200,257+(d1/6),(int) (300*(fmin/fc)),4*d1/6);
		g22.setColor(Color.orange);
		g22.fillRect(200,257+5*d1/6,(int) (300*(fmin/fc)),d1/6);
		
	}
	
	public void for_te2(Graphics g22, int i){
		g22.setColor(Color.orange);
		g22.fillRect(200,257,(int) (300*(fmin/fc)),d1/10);
		g22.setColor(Color.red);
		g22.fillRect(200,257+(d1/10),(int) (300*(fmin/fc)),3*d1/10);
		g22.setColor(Color.orange);
		g22.fillRect(200,257+4*d1/10,(int) (300*(fmin/fc)),d1/10);
		g22.setColor(Color.cyan);
		g22.fillRect(200,257+5*d1/10,(int) (300*(fmin/fc)),d1/10);
		g22.setColor(Color.blue);
		g22.fillRect(200,257+6*(d1/10),(int) (300*(fmin/fc)),3*d1/10);
		g22.setColor(Color.cyan);
		g22.fillRect(200,257+9*d1/10,(int) (300*(fmin/fc)),d1/10);
		
	}
	
	public void for_te3(Graphics g22, int i){
		g22.setColor(Color.orange);
		g22.fillRect(200,257,(int) (300*(fmin/fc)),d1/15);
		g22.setColor(Color.red);
		g22.fillRect(200,257+(d1/15),(int) (300*(fmin/fc)),3*d1/15);
		g22.setColor(Color.orange);
		g22.fillRect(200,257+4*d1/15,(int) (300*(fmin/fc)),d1/15);
		g22.setColor(Color.cyan);
		g22.fillRect(200,257+5*d1/15,(int) (300*(fmin/fc)),d1/15);
		g22.setColor(Color.blue);
		g22.fillRect(200,257+6*(d1/15),(int)(300*(fmin/fc)),3*d1/15);
		g22.setColor(Color.cyan);
		g22.fillRect(200,257+9*d1/15,(int) (300*(fmin/fc)),d1/15);
		g22.setColor(Color.orange);
		g22.fillRect(200,257+10*d1/15,(int) (300*(fmin/fc)),d1/15);
		g22.setColor(Color.red);
		g22.fillRect(200,257+11*(d1/15),(int) (300*(fmin/fc)),3*d1/15);
		g22.setColor(Color.orange);
		g22.fillRect(200,257+14*d1/15,(int) (300*(fmin/fc)),d1/15);
	}

	public void actionPerformed(ActionEvent ae) {
		// TODO Auto-generated method stub
		
		theta+=0.5;
		 
	    repaint();
			try{
			
				if(ae.getActionCommand()!= null){
					System.out.println("Event"+ae.getActionCommand()); 	
			 		if(ae.getActionCommand().equals("submit")){ 
			 				actionPerformed2();
			 				
			 		}
			 			if(ae.getActionCommand().equals("TEm")){ 
			 				cnt=1;
			 				
			 				}
			 			
			 			 if(ae.getActionCommand().equals("TMm")){
				 			cnt=2;
				 			}
				 		
			 			 if(ae.getActionCommand().equals("TEM")){
					 		cnt=3;
						 	}
			 			 
			 		/*	 if(ae.getActionCommand().equals("TE1")){
			 				System.out.println("Event"+ae.getActionCommand()); 
						 		cnt=4;
							 	}*/
			 			 
			 			if(ae.getActionCommand().equals("TE2")){
			 				System.out.println("Event"+ae.getActionCommand()); 
					 		cnt=5;
						 	}
			 			
			 			if(ae.getActionCommand().equals("TE3")){
			 				System.out.println("Event"+ae.getActionCommand()); 
					 		cnt=6;
						 	}
			 			
				}	
				}catch(Exception e){ e.printStackTrace();}
			}
		
	
	
		public void actionPerformed2(){
		
			System.out.println("Into ActionPerformed2");
	 
			d1 = Integer.parseInt(D1.getText());
			f1 = Double.parseDouble(F1.getText());
		}
		
		
	 		}

while checking the code give distance = 100 and frequency=1;
i am using eclipse for this

Thanking you....

Use Buttons instead of JButtons as you are using an applet

Sorry i m here again.... I am using JApplet in program.
While writing i mentioned it as applet but in program I am using JApplet.

Sorry i m here again.... I am using JApplet in program.
While writing i mentioned it as applet but in program I am using JApplet.

Figured out that your buttons TE1, TE2, TE3 are not listening
Sorry cant get what the actual problem in your code. But just by hit n trail by using Buttons instead of JButtons all your buttons are Listening

Figured out that your buttons TE1, TE2, TE3 are not listening
Sorry cant get what the actual problem in your code. But just by hit n trail by using Buttons instead of JButtons all your buttons are Listening

Thanks for your reply.
Somebody has told me there is problem with layout.
But no idea whats exact or about solution???
:'(

i can't get it clear...

ok i think you have N number of Jbutton[1]....Jbutton[N]..

after you click Jbutton[1] then it will call actionPerformed{} for corresponding Jbutton[1]..

i asking 2 question to you.

1.you call same action performed for all buttons from 1..to..N..

2.else you need respectively..like b[1]-->actperform[1] ..

because ActionPerformed method need to get which button need response from that..

that's why method get conflict about response go to which button...

this only as far as i understood...

you make it clear then i will try to solve it...

i can't get it clear...

ok i think you have N number of Jbutton[1]....Jbutton[N]..

after you click Jbutton[1] then it will call actionPerformed{} for corresponding Jbutton[1]..

i asking 2 question to you.

1.you call same action performed for all buttons from 1..to..N..

2.else you need respectively..like b[1]-->actperform[1] ..

because ActionPerformed method need to get which button need response from that..

that's why method get conflict about response go to which button...

this only as far as i understood...

you make it clear then i will try to solve it...

thanks a lot musthafa.aj for your reply...
the Jbutton array was creating many problems so now i have written a simple program ( given in post :8).
again there is problem with actionlistener or with layout. i am sure abt it.
please go through post no.8

i will little bit busy now..

so you please give link for post no 8...i can't get it..

otherwise pms to me....it will be better...

i will little bit busy now..

so you please give link for post no 8...i can't get it..

otherwise pms to me....it will be better...

see the post i m writing right now is post no. 18
just have to look at post no. 8 from the same thread ( this thread).
if i will give you a link it will open the same page.
just go up in this thread or visit page.no.1 of the same thraed.

thanksss

////////////////////////////////////////////////////////////////////////////
this is your code


buttons[j].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){

/////Don't write methode implementation here......

}


////////////////////////////////////////////////////////////////////////////

actually what we need to do is....

buttons[j].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){

[B]your_method[/B](e);

}


private void [B]your_method[/B](ActionEvent e)
    {

        Add any handling code here

    }

this what till i thought as your problem...

anything reply i will try as much i c:icon_cool:an...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.