Results 1 to 3 of 3

Thread: Help straightning out code for an array of buttons

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Resolved Help straightning out code for an array of buttons

    I want to create an array of 9 buttons (btn[3][3]) and then check to see if the button at index [2][2] was clicked. I just cant seem to get the code to work. Any help is appreciated.

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ButtonArray extends JFrame implements ActionListener {
      JButton btn[][];
    
      public ButtonArray() {
    	Container pane = getContentPane();
    	FlowLayout flo = new FlowLayout();
    	setSize(300,300);
    	setTitle("Array of Buttons");
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    	//here I want to create 9 buttons and add an actionListener to them and add them to the pane
    
    	for (int x=0; x<3; x++) {
    		for (int y=0; y<3; y++) {
    			btn[x][y] = new JButton();
    			btn[x][y].addActionListener(this);
    			pane.add(btn[x][y]);
    		}
    	}
    
            pane.setLayout(flo);
    	setContentPane(pane);
    	setVisible(true);
       }
    
    
       public void actionPerformed(ActionEvent e) {
    
    	//I would like to check to see if the button that was clicked is at [2][2]
    	//If so just a simple println method
    
    	
    	for (int x=0; x<3; x++) {
    	    for (int y=0; y<3; y++) {
    		if (e.getSource == btn[2][2]) {
    			System.out.println("Button at index[2][2] was clicked");
    		}
    	    }
    	}
       }
     
       public static void main(String[] args) {
    		ButtonArray ba = new ButtonArray();
       }
    }
    Last edited by Dilenger4; Oct 7th, 2004 at 05:13 PM.

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class ButtonArray extends JFrame implements ActionListener {
      JButton btn[][];
    
      public ButtonArray() {
       btn = new JButton[3][3]; 
       JPanel jp = new JPanel(); 
       setSize(300,150);
       setTitle("Array of Buttons");
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
       for(int x=0; x<3; x++) {
        for(int y=0; y<3; y++) {
         btn[x][y] = new JButton("JButton");
    	btn[x][y].addActionListener(this);
             jp.add(btn[x][y]);
        }
       }
        
        setContentPane(jp);
        setVisible(true);
      }
    
      public void actionPerformed(ActionEvent e) {
       for(int x=0; x<3; x++) {
        for(int y=0; y<3; y++) {
         if(e.getSource() == btn[2][2]) {
    	System.out.println("Button at index[2][2] was clicked");
        }
       }
      }
     }
     
      public static void main(String[] args) {
       ButtonArray ba = new ButtonArray();
      }
     }

  3. #3

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111
    Thanks. Works perfectly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width