简体   繁体   中英

Adding components to JFrame in Netbeans Swing GUI Builder

I am trying to create my application using Netbeans GUI Builder, but I have a situation here.

When I drag and drop a component(Jlabel or any other used defined component) to JPanel from Palette window of GUI Builder, the Java code is automatically added by the Netbeans. For eg. the following code is generated:

**private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

}**

Now I have a ArrayList which need to store the component objct which is added by GUIBuilder. In this case the object added is jLabel1.

ArrayList updateComponentsList = new ArrayList();

So I need to store this object in this ArrayList. Infact I need the new component object to be automatically added to this list, whenever a new component is added by GUIBuilder.

Since GUIBuilder generates the Java code for added component automatically, how do I make GUIBuilder to update this ArrayList automatically whenever a new component is added?

Can anybody please help me to figure this out?

Thanks in advance.

It might work for you. When new components are added in your current GUI, it automatically calls the initComponent() method to redraw the JFrame and you can get the updated list of component by calling this below method at the end of initComponent() block.

public static List getAllComponents(final Container c) {

Component[] comps = c.getComponents();
List<Component> compList = new ArrayList<Component>();
for (Component comp : comps) {
  compList.add(comp);
  if (comp instanceof Container) {
    compList.addAll(getAllComponents((Container) comp));
  }
}
return compList;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM