Dynamic display with JGoodies
The basics of using the JGoodies Forms package are really well described in their excellent Whitepaper, however, this doesn’t really cover how to add and remove elements dyamically.
The recommended builder approach works well for constructing the first display of the page, but updates need a little bit more work. When updating the display I have fallen back to updating the panel more directly.
This involves two steps, first removing the components from the swing container, then adding the new components, specifying FormLayout constraints.
The remove componets process is:
go to the container,
call getComponents,
iterate until a known component is reached.
All components after this are removed using remove.
Each new component to add, has the following process applied.
First add a row to the layout,
layout.appendRow(new RowSpec(”pref”));
then add the label, and field.
panel.add(label, constraints.xy(row, 1))
panel.add(field, constraints.xy(row, 3))
The constraints object is an instance of the JGoodies CellConstraints class.
layout is a JGoodies FormLayout. The rowSpec specifies how the row is to be layed out (here we are respecting the preference of the field).
Using this approach gives well ordered look. The dynamic section of the form is seperated from the non-dynamic, but doesn’t look like it was tacked on.