Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to register array of button and identify in actionPerfomed method.

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to register array of button and identify in actionPerfomed method.

    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?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

Similar Threads

  1. bit array and register problem
    By ryu2le in forum What's Wrong With My Code?
    Replies: 35
    Last Post: August 31st, 2011, 10:52 PM
  2. How to identify a roman numeral
    By dunWorry in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 20th, 2010, 12:58 AM
  3. result set array button
    By dread_arisawa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 21st, 2010, 10:05 AM
  4. Cash Register Exercise
    By Consus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 19th, 2010, 11:52 PM
  5. Identify and avoid some of the pitfalls in learning to use generics
    By JackyRock in forum Java Theory & Questions
    Replies: 0
    Last Post: February 6th, 2010, 05:12 AM

Tags for this Thread