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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:09:34+00:00 2026-06-18T22:09:34+00:00

I have a JPanel on which I’ve dynamically added quite a few JButtons. All

  • 0

I have a JPanel on which I’ve dynamically added quite a few JButtons. All of this is working perfectly. Later on in my program execution, I need to refer back to these buttons and pull out the button text. I’m having trouble figuring out how to refer back to them.

When I created each button, I gave it a unique name. Let’s say this is the code where I created the button:

public void createButton(Container parent, String btnName) {
   JButton btn = new JButton("xyz");
   btn.setName(btnName);
   btn.addActionListner(new ActionListner() {
      //code
   }
   parent.add(btn);
 }

In another method, I’m trying to retrieve the label on the button since it may have changed at run time. Do I need to keep an array of these buttons as they are created? Or is there a way that I can refer back to them directly?

This is what I was working on, but it’s stupid. Can anyone suggest a correct approach?

public String getBtnLabel(String btnName) {
   JButton btn = (JButton) btnName;
   return btn.getText();
}

If the answer is that I just need to create the array and then iterate over it, that’s fine. Just looking for other options.

  • 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-18T22:09:36+00:00Added an answer on June 18, 2026 at 10:09 pm

    You need to use a Map<String, JButton> so when you create your dynamic buttons you give them some sort of unqiue name:

    //somewhere at the top of your class
    private final Map<String, JButton> myButtonMap = new HashMap<>();
    
    public void createButton(Container parent, String btnName) {
       JButton btn = new JButton("xyz");
       btn.setName(btnName);
       btn.addActionListner(new ActionListner() {
          //code
       }
       parent.add(btn);
       myButtonMap.put(btnName, btn);
     }
    

    And then simply get from the map

    public String getBtnLabel(String btnName) {       
       return myButtonMap.get(btnName).getText();
    }
    

    This will obviously throw an NPE if the button isn’t defined…
    Also you will need to delete from your map when you’re done with it otherwise you’re asking for a memory leak…

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

Sidebar

Related Questions

I have a program which creates JButtons which are then added to a JPanel
I have a JFrame(frm) in which I've added a JPanel (pnl1)..which in turn has
Hi Experts: I have a class which extends JPanel now ShapePanel (Sp) this gets
I have a JPanel which has a background image. To this panel, I add
I have a JPanel and i am creating JButtons dynamically on runtime. I want
I have a JPanel which is set to BorderLayout . I have added 2
I have a JPanel that displays a group of JLabels which are all computed
So in my program I have a JPanel ( called squareContainer ) which has
I have a JPanel which uses a BoxLayout in the X_AXIS direction. The problem
I have a JPanel which contains some fields. The height of the JPanel is

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.