Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8502623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:32:14+00:00 2026-06-11T01:32:14+00:00

I have created a grid that contains 10×10 buttons using 2d arrays. i tried

  • 0

I have created a grid that contains 10×10 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].addActionListener(this);
                grid[x][y].setLabel(charput); //set charput as label
            }
        }
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        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
        }
        else
        {
            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])
            JOptionPane.showMessageDialog(null,x.getSource().getLabel);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:32:15+00:00Added an answer on June 11, 2026 at 1:32 am

    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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a grid in a doGet() function. This grid contains a dynamic
I have created an android application that calls (using kSOAP library) a SOAP based
I have just created a grid application using the template provided in Visual Studio
I have an ObservableCollection that is created in user control that contains a custom
I am using MVVM Light. I have created a window that looks like this:
I have a GridView with dynamically created image buttons that should fire command events
I have a ControlTemplate that contains a XamWebChart . For each pie slice created
I have about 20 grid views that I have to create. All of them
I have a Windows 8 Metro application created from the Grid Application template. I
i have one question about jquery ajax().. I created a table grid and there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.