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 6725457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:48:34+00:00 2026-05-26T09:48:34+00:00

I have scoured the internet but can’t find an answer to this : I’m

  • 0

I have scoured the internet but can’t find an answer to this :

I’m using a for loop to create 36 buttons called a1, a2, etc. and assigning each of them a unique Action Command at the same time.

Later on I wanted to get the name of the button from the actionPerformed(ActionEvent e) method.

I could get the ActionCommand easy enough, but I need the name of the button as well.

Any help much appreciated!

Edit:

Here is the code I’m using:

String letters[] = {"0", "a", "b", "c", "d", "e", "f"};
JButton btn[] = new JButton[35];
int count = 0;


for (int f=1; f < 7;f++){

        for (int i=1; i < 7;i++){
            btn[i] = new JButton(letters[f]+i, cup);
            System.out.println(btn[i]));
            mainGameWindow.add(btn[i]);
            btn[i].addActionListener(this);
            String StringCommand = Integer.toString(randomArrayNum());
            btn[i].setActionCommand(StringCommand);
            count++;
            if(count == 18){
                generateArray();
            }

        }

}

This gives you 36 buttons for a 6×6 grid that go a1-6, b1-6, c1-6 etc

I just can’t seem to control the buttons once I’ve created them this way, I can’t assign icons or get the name of the button.

Thanks in Advance.

  • 1 1 Answer
  • 1 View
  • 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-05-26T09:48:35+00:00Added an answer on May 26, 2026 at 9:48 am

    Keep a reference of the buttons in a Map

    String letters[] = {"0", "a", "b", "c", "d", "e", "f"};
    JButton btn;
    int count = 0;
    
    HashMap<String,JButton> buttonCache = new HashMap<String,JButton>();
    
    
    for (int f=1; f < 7;f++){
    
        for (int i=1; i < 7;i++){
            btn = new JButton(letters[f]+i, cup);
            mainGameWindow.add(btn[i]);
            btn.addActionListener(this);
            String stringCommand = Integer.toString(randomArrayNum());
            btn.setActionCommand(stringCommand);
            buttonMap.put(stringCommand,btn);
            count++;
            if(count == 18){
                generateArray();
            }
    
        }
    
    } 
    

    Then, in your ActionListener, get the button back from the command :

    public void actionPerformed(ActionEvent e) {
        String command = ((JButton) e.getSource()).getActionCommand();
        JButton button = buttonCache.get(command);
        if (null != button) {
            // do something with the button
        }
    }
    

    Edit

    Revisiting this answer over five years later, I have no idea why I suggested a HashMap 😛

    This code does the exact same thing, no third party Map :

    String letters[] = {"0", "a", "b", "c", "d", "e", "f"};
    int count = 0;
    
    for (int f=1; f < 7;f++){
        for (int i=1; i < 7;i++) {
            String stringCommand = Integer.toString(randomArrayNum());
            Button btn = new JButton(letters[f]+i, cup);
    
            btn.setActionCommand(stringCommand);
            btn.addActionListener(this);
            mainGameWindow.add(btn[i]);
    
            // NOTE : I have no idea what this is for...
            count++;
            if(count == 18){
                generateArray();
            }
        }
    }
    

    in the ActionListener…

    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton) e.getSource();
        String command = button.getActionCommand();
    
        // do something with the button
        // the command may help identifying the button...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have scoured the internet looking for an answer to this question and can't
I have scoured the internet for this answer, and I feel like I keep
I have scoured the Internet for an answer to this. I have an array
I have scoured the internet to find which CMS's run on CloudSites and the
I have scoured Google for a tutorial to help with this but haven't been
I have scoured the internet for code that can be used to allow users
I have scoured the internet looking for a solution to this and I am
I'm new at BeautifulSoup and have scoured the online docos but couldn't find what
I have scoured the web in search of a solution to this problem, but
I've scoured the internet for help on this problem but I need help. This

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.