Results 1 to 2 of 2

Thread: how to get the row/col for these buttons

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    how to get the row/col for these buttons

    Code:
    	// Tic Tac Toe game panel.
    	JPanel boardGamePanel = new JPanel();
    	boardGamePanel.setLayout(new GridLayout(3, 3));
    	
    	JButton button[][] = new JButton[3][3];
    
    	for (int row = 0; row < ROW; row++){
    	    for (int col = 0; col < COL; col++){
    
    		button[row][col] = new JButton();
    
    		try {
    		    button[row][col].setIcon(new ImageIcon(new URL("http://localhost:8080/eg/Images/blank.gif")));
    		}
    		catch (Exception e) { }
    		
    		button[row][col].setBackground(Color.white);
    		button[row][col].addActionListener(this);
    
    		boardGamePanel.add(button[row][col]);
    	    }
    	}
    
    	contentPane.add(boardGamePanel);
    As you see, button[row][col].addActionListener(this);

    but when i click on the button, how would i actually get the row and col to keep track of the coordinates that have been clicked on the grid?

    Code:
        public void actionPerformed(ActionEvent e) {
    	// when the button is clicked i want to get the row/col for that button too
        }

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: how to get the row/col for these buttons

    a nested for loop on the button array then compare array items to e.getSource() if they are the same object then you've got your button
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

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