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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:43:29+00:00 2026-05-27T04:43:29+00:00

My question is how to access the Swing GUI element tree (main window, JPanels,

  • 0

My question is how to access the Swing GUI element tree (main window, JPanels, JFrames, JButtons, JTextFields ect) and create a reference to that tree. I need this to be kept in a data structure (ex. hash map) and NOT in a memory file (eg. using serialization). I need this for using it later to map these UI elements to the corresponding objects inside the code.

EDIT:

   JFrame f = new JFrame("Basic GUI"); 
   JPanel pnl1 = new JPanel(); 
   JPanel pnl2 = new JPanel(); 
   JPanel pnl3 = new JPanel(); 

   JLabel lblText = new JLabel("Test Label");
   JButton btn1 = new JButton("Button");
   JTextField txtField = new JTextField(20);

   public GUISample(){

   pnl1.add(lblText);
   pnl2.add(btn1);
   pnl3.add(txtField);

   f.getContentPane().setLayout(new BorderLayout());
   f.getContentPane().add(pnl2, BorderLayout.EAST);
   f.getContentPane().add(pnl3, BorderLayout.WEST);
   f.getContentPane().add(pnl1, BorderLayout.NORTH);
   visitComponent(f);

   }

   private Map<String, Component> hashMap = new HashMap<String,Component>();

   public Map<String, Component> getComponentsTree(){
      return hashMap;
   }
   public void visitComponent(Component cmp){
      // Add this component
      if(cmp != null) hashMap.put(cmp.getName(), cmp);
      Container container = (Container) cmp;
      if(container == null ) {
          // Not a container, return
          return;
      }
      // Go visit and add all children
      for(Component subComponent : container.getComponents()){
          visitComponent(subComponent);
      }
     System.out.println(hashMap); 

    }
  • 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-27T04:43:30+00:00Added an answer on May 27, 2026 at 4:43 am

    I have been thinking about this problem. Here is my suggestion:

    public class ComponentTreeBuilder{
       private Map<String, Component> hashMap = new HashMap<String,Component>();
       public Map<String, Component> getComponentsTree(){
          return hashMap;
       }
       public void visitComponent(Component cmp){
          // Add this component
          if(cmp != null) hashMap.put(cmp.getName(), cmp);
          Container container = (Container) cmp;
          if(container == null ) {
              // Not a container, return
              return;
          }
          // Go visit and add all children
          for(Component subComponent : container.getComponents()){
              visitComponent(subComponent);
          }
       }
    }
    

    And use this like this:

    Frame myFrame = new JFrame();
    // Make sure you add your elements into the frame's content pane by
    myFrame.getContentPane(component);
    ComponentTreeBuilder cmpBuilder = new ComponentTreeBuilder();
    cmpBuilder.visitComponent(myFrame);
    Map<String, Component> components = cmpBuilder.getComponentsTree(); 
    // All components should now be in components hashmap
    

    Please note that ComponentTreeBuilder is using recursion, this might throw a stack overflow exception if you have too many components in your GUI.

    EDIT Just tested this code on real example and…. it works 🙂

    Here is the code:

    JFrame frame = new JFrame();
    frame.getContentPane().add(new JButton());
    frame.getContentPane().add(new JButton());
    frame.getContentPane().add(new JButton());
    frame.getContentPane().add(new JButton());
    visitComponent(frame);
    

    And here is the output:

    enter image description here

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

Sidebar

Related Questions

I have a question about Access.If for example I have a table with the
This is more an observation than a real question: MS-Access (and VBA in general)
Basically this is a question how to access local scope handler. I trying to
I just came across the question - is the access of an object safely
Question: Should I write my application to directly access a database Image Repository or
My question concerns c# and how to access Static members ... Well I don't
my question is about two possible ways to access data: (My question is about
Basic question - is it possible to access the current Page from a static
My question about the reconfiguration delay when switching between Access 2003 and 2007 the
My question is pretty straightforward: You are an executable file that outputs Access granted

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.