VwpFAQDynamicAddComp

Group Panel: How can I dynamically add components to a group panel?

Below are steps to programatically add a static text component to a group panel.


1. Add a button, and a group panel
2. Right click on the Group Panel component in the designer and select Add Binding Attribute.
3. Double click on the SessionBean1 node in the Projects window to open the Java editor.
4. In the Java editor, right click in an empty space and select Insert Code... > Add Property...
5. Add property named counter with type int and initial value 1.

    Image:addCounter_VwpFAQDynamicAddComp.jpg


6. Go back to the outline designer view for page1, double click the button and add the following source to the action() method:

         int count = getSessionBean1().getCounter();

         UIComponent parent = groupPanel1;
         StaticText newOne = new StaticText();
         newOne.setId("newOne" + count);
         newOne.setText("This is statictext" + count);
         boolean childAdded = parent.getChildren().add(newOne);
         getSessionBean1().setCounter(count + 1);


7. Fix imports
8. Deploy/Run the application

Each press of the button will add a new staticText to the group panel.

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo