Coding Examples

  Adding components to a design grid
  Setting combo box items
  Setting list items
  Setting tree data
  Setting table data
  Controlling card panels

Most applications process some dynamic data, unknown until runtime. When coding the UI by hand, you generally connect the (data) view components to the data at the time they are constructed, with code like new JTable(model) or new JList(items). With RADi you can not do this because, at the time your code executes, the GUI already is constructed. So this section shows some ways of updating the data (model) of lists, trees and tables.

Sometimes your only choice is to add a component to the layout at runtime. Read on if you need to know how to add a component to a design grid.

Adding components to a design grid     Top of page

Sometimes you may want to add some component to the UI at runtime. For frames and dialogs, you should do this before the window is packed. The perfect place for your code is inside the constructor, just before the call to setupFrame respective setupDialog.

All internal panels, folder panels, the top container and internal frames have a design grid, which actually is a NewLayout layout manager. First you need a reference to the container (this is either the top container, the internal panel, the folder panel or an internal frame's content pane). For this container you must then call the two-argument add method with the component to add as the first argument and a NLConstraint object as the second argument, for example:
container.add(component, NLConstraint.rc(2, 0));
will add the component to the third row/first column of the grid (with all anchors set).

To add a component which spans two columns and whose left and right anchors are set, code:
container.add(component, NLConstraint.rcwh(2, 0, 2, 1, "lr"));

You must also know that for top containers and internal frames which contain a menu bar or a floating tool bar (or both), the grid changes at runtime because the menu bar and the tool bar are removed from the grid. This means that a grid with 4 rows and a menu bar in the first row will have only 3 rows at runtime (row number 1 will become row number 0).

To not make it too simple, there is one more exception for internal frames: An internal frame with a floating tool bar adds this tool bar to a BorderLayout. The border layout is set for the content pane and has two children: the tool bar and a JPanel containing the design grid. To add a component to the grid, you must first get a reference to the JPanel, using code like this:
Container container = null;
Component c = internalFrame.getContentPane().getComponent(0);

if(c instanceof JPanel) {
    container = (Container)c;
}
else {
    container = (Container)internalFrame.getContentPane().getComponent(1);
}

You find more information about NewLayout and NLConstraint in the RADi runtime Javadoc at .../RADi/javadocs.


Setting combo box items     Top of page

Normally you define a combo box' content in the constructor, either with JComboBox(Object[] items) or with JComboBox(Vector items).

If the combo box is already constructed you have three choices:
Define your own ComboBoxModel and set it with
combo.setModel(new MyComboBoxModel());
Set the data with
combo.setModel(new DefaultComboBoxModel(Object[] items));
Set the data with
combo.setModel(new DefaultComboBoxModel(Vector items));


Setting list items     Top of page

Analogous to a combo box you normally define a list's content in the constructor, either with JList(Object[] items) or with JList(Vector items).

If the list is already constructed you have three choices:
Define your own ListModel and set it with
list.setModel(new MyListModel());
Set the data with
list.setListData(Object[] items);
Set the data with
list.setListData(Vector items);


Setting tree data     Top of page

The most flexible way to set a tree's data is to define its model. A custom tree model class either has to implement the TreeModel interface or extend DefaultTreeModel, your choice should depend on the kind of data you are representing.

Every tree created in RADi is backed by a DefaultTreeModel. So you can also define a tree's data by creating a root node (as an instance of DefaultMutableTreeNode), then adding other nodes as needed and finally setting the model's root node with
((DefaultTreeModel)tree.getModel()).setRoot(myRootNode);


Setting table data     Top of page

With RADi you can define only static table models. If you did define a table and you want to preserve column and header renderers while updating the data, refer to Feed a table with your own data.

Every table defined in RADi is backed either by a DefaultTableModel or by a RadiTableModel (which is derived from DefaultTableModel). So, to completely redefine table data you have three choices:
Define your own TableModel and set it with
table.setModel(new MyTableModel());
Set the data with
((DefaultTableModel)table.getModel()).setDataVector(
    Object[][] dataVector, Object[] columnIdentifiers);
Set the data with
((DefaultTableModel)table.getModel()).setDataVector(
    Vector dataVector, Vector columnIdentifiers);


Controlling card panels     Top of page

With RADi you can define a controller for a card panel allowing you to switch cards without programming. Controllers can be one of JList, JComboBox or JTree (see Define a controller for a CardPanel).

Sometimes you may want to control a card panel with a group of buttons, then follow these steps:
In RADi insert the card panel and add cards as needed (you may also want to edit the CardLayout's ).
For every card insert a selectable button in the layout (radio buttons, toggle buttons or radio menu items). Then, for each button, set its actionCommand to the constraint of the card you want to be displayed as the button is clicked.
Create a button group from all controller buttons (so only one can be selected at a time). To do this, select all buttons for the group and execute 'Create Button Group' from the grid's or from the 'Edit' menu.
For every controller button define an whose actionPerformed() method is forwarded to the same target method (switchCard()). It is important that the ActionEvent is forwarded.
Export the layout, then edit the source code like this:
protected void switchCard(ActionEvent e) {
    // Get the constraint from the event source
    String constraint = ((AbstractButton)e.getSource()).getActionCommand();

    // Select the card
    ((CardLayout)cardPanel.getLayout()).show(cardPanel, constraint);
}