I have a class that extends JPanel. In the constructor I have created a GridBagLayout and filled it with some JButtons. But I'm actually trying to create a GUI that will change it's buttons at runtime based on user selections. This would require varying the size of the panel as some options require more buttons than others. Is it possible then, to change the components in a GridBagLayout on the fly? If so, how?
I would be very grateful for some insight on this.
gridBagLayoutPanel.remove(component); and/or setConstraints gridBagLayoutPanel.add(component,constraints); gridBagLayoutPanel.revalidate();//forces panel to lay out components again gridBagLayoutPanel.repaint();//sometimes needed, sometimes not. Safest to include
I always feel dumb when trying to answer questions like this about GridBagLayout. Java is a much better language than English when it comes to dealing with the subtlety of this beautiful class. Is it possible then, to change the components in a GridBagLayout on the fly? Yes. If so, how? The removal part is easy, as Michael has shown. For adding a button back to the parent container there can be some added details. To simply add it to the end (last place) of the children, Michael's suggestion should work well. To insert a button into the hierarchy of child components you will need to specify its new position. One way to do this is to:
set the constraints for the button (which were lost when it was removed) with
determine the index in the parent's component hierarchy (its children) for the insertion
add the button with
parentContainer.add(Component c, int index) The next question is what constraints to use for adding the button back into the parent. It's been awhile since I worked with this so I made up a small exploratory app for testing. It seems that when a component is removed from its parent the parent's gridbagLayout discards the constraints that were set for the component. Querying the gridbagLayout for the constraints of the removed button gridbagLayout.getConstraints(Component c) returns a default GridBagConstraints object. How you want to proceed really depends on what you are trying to do. In general tems, some options for determining constraints for the button we are adding might include: 1 — caching the original constraints for each button in an array (or Collection if you like). When adding the button, lookup its cached constraints in the array and set these for the button. One drawback with this approach is that it ignores the state of the layout. 2 — add the button to the parent at the desired (sibling) index and then run through all child components, resetting the constrains of/for each component. 3 — add the component to the parent, get a reference to the components with parent.getComponents(), remove all components and add them back with new constraints. This is the easiest and most straight–forward option. Any of these options would be followed by a (dutiful) call to revalidate and, in this case (though not always required), repaint on the parent container.
I think she's lovely. It's this tiny ad that called her crazy: