Kenzo-kun Kenzo-kun - 1 year ago 56
Java Question

How to know what component was clicked from a grid?

I have created a grid that contains 10x10 buttons using 2d arrays. i tried x.getSource().getLabel() but compiler says they are not compatible. also i want to get the specific button that was clicked.

I want to get the exact button that was clicked from the grid i made and get its label. what method i need to use?

import javax.swing.JFrame; //imports JFrame library
import javax.swing.JButton; //imports JButton library
import java.awt.GridLayout; //imports GridLayout library
import javax.swing.*;
import java.awt.event.*;
import java.util.*;

public class ButtonGrid extends JFrame implements ActionListener
JFrame frame=new JFrame(); //creates frame
JButton[][] grid; //names the grid of buttons
public int x;
public int y;
public ButtonGrid(int width, int length)
{ //constructor
char temp;
String charput;
frame.setLayout(new GridLayout(width,length)); //set layout
grid = new JButton[width][length]; //allocate the size of grid
for(int y=0; y<length; y++)
{ //start
for(int x=0; x<width; x++)
temp=charRand(); //get random character
charput = ""+temp; //converts character to string
grid[x][y]=new JButton(); //creates new button
frame.add(grid[x][y]); //adds button to grid
grid[x][y].setLabel(charput); //set charput as label
frame.pack(); //sets appropriate size for frame
frame.setVisible(true); //makes frame visible
/* generates randomiz letter for the button of the grid*/
public char charRand()
String consonantList = new String("BCDFGHL"); //list 1
String consonantList2 = new String("MNPRSTWY"); //list 2
String consonantList3= new String("JQXZVK"); //list 3
String vowelList = new String("AEIOU"); //list of vowels
int vowelOrConsonant; //holder of random number
int chosen; //selects the chosen random letter
Random randGen = new Random(); //generates random int value
char selected; //gets the random letter chosen by variable chosen

vowelOrConsonant = randGen.nextInt(4);
if (vowelOrConsonant == 0)
chosen = randGen.nextInt(5); //list of vowels
selected = vowelList.charAt(chosen); //selects a char from vowels
else if(vowelOrConsonant == 1)
chosen = randGen.nextInt(7); //list 1
selected = consonantList2.charAt(chosen); //selects a char
else if(vowelOrConsonant == 2)
chosen = randGen.nextInt(8); //list 2
selected = consonantList2.charAt(chosen); //selects a char
chosen = randGen.nextInt(6); //list 3
selected = consonantList.charAt(chosen);
return selected; //returns the random letter

public static void main(String[] args)
new ButtonGrid(10,10);//makes new ButtonGrid with 2 parameters

public void actionPerformed(ActionEvent x)
/* i get wrong output on this line.
* i want to get the exact button that was clicked and get its label.
if (x.getSource()==grid[x][y])

Answer Source

getSource() returns an Object, so you need to cast it to JButton, like this:

public void actionPerformed(ActionEvent x) {
    JOptionPane.showMessageDialog(null, ((JButton)x.getSource()).getText());

Also note that getLabel() and setLabel() are deprecated and should be replaced by getText() and setText().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download