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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:14:00+00:00 2026-05-25T06:14:00+00:00

I have in a JFrame some components that I want to refer into another

  • 0

I have in a JFrame some components that I want
to refer into another JFrame and I want
to get them by name and not
do public get/set methods for each.

Is there a way from Swing to get a component reference by its name like do
c#?

e.g. form.Controls["text"]

Thanks

  • 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-05-25T06:14:01+00:00Added an answer on May 25, 2026 at 6:14 am

    I know this is an old question, but I found myself asking it just now. I wanted an easy way to get components by name so I didn’t have to write some convoluted code each time to access different components. For example, having a JButton access the text in a text field or a selection in a List.

    The easiest solution is to make all of the component variables be class variables so that you can access them anywhere. However, not everyone wants to do that, and some (like myself) are using GUI Editors that don’t generate the components as class variables.

    My solution is simple, I’d like to think, and doesn’t really violate any programming standards, as far as I know (referencing what fortran was getting at). It allows for an easy and straightforward way to access components by name.

    1. Create a Map class variable. You’ll need to import HashMap at the
      very least. I named mine componentMap for simplicity.

      private HashMap componentMap;
      
    2. Add all of your components to the frame as normal.

      initialize() {
          //add your components and be sure
          //to name them.
          ...
          //after adding all the components,
          //call this method we're about to create.
          createComponentMap();
      }
      
    3. Define the following two methods in your class. You’ll need to import Component if you haven’t already:

      private void createComponentMap() {
              componentMap = new HashMap<String,Component>();
              Component[] components = yourForm.getContentPane().getComponents();
              for (int i=0; i < components.length; i++) {
                      componentMap.put(components[i].getName(), components[i]);
              }
      }
      
      public Component getComponentByName(String name) {
              if (componentMap.containsKey(name)) {
                      return (Component) componentMap.get(name);
              }
              else return null;
      }
      
    4. Now you’ve got a HashMap that maps all the currently existing components in your frame/content pane/panel/etc to their respective names.

    5. To now access these components, it is as simple as a call to getComponentByName(String name). If a component with that name exists, it will return that component. If not, it returns null. It is your responsibility to cast the component to the proper type. I suggest using instanceof to be sure.

    If you plan on adding, removing, or renaming components at any point during runtime, I would consider adding methods that modify the HashMap according to your changes.

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

Sidebar

Related Questions

I have a JPanel that I want to add some components. in particular JButton
I have a JFrame with some a JTextField and a JButton. I want it
I have a JPanel that is not part of a JFrame. For various reasons
I want to add some new Components to my JFrame during runtime when a
I have a JFrame with 1 panel for drawing and set KyeListener for JFrame.
I have a JFrame and I open a JDialog from it and another JDialog
I have another beginner's question that hopefully someone can help with. I'm trying to
so I have some code that looks like this: import java.awt.BorderLayout; import java.awt.Component; import
I have a problem. I have a JFrame with some JTextFields, JLabels, Jlists &
I have a Main app window (JFrame) with JPanel. On JPanel I have some

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.