Geertjan is a DZone Zone Leader and has posted 469 posts at DZone. You can read more from them at their website. View Full User Profile

Getting Even Further with Spring RCP (1)

  • submit to reddit

Replacing the Table with a Tree

In the previous part, we displayed our data in a table, using the Spring RCP "AbstractObjectTable" class. This time, instead, we'll display our data in a tree:


  1. Start by simply pasting the following into the existing CustomerView class:
    private void createTree() {
    DefaultMutableTreeNode rootNode =
    new DefaultMutableTreeNode("Customers");
    Object[] cutomers = customerDataStore.getAllCustomers();
    for (int i = 0; i < cutomers.length; i++) {
    Customer customer = (Customer) cutomers[i];
    DefaultMutableTreeNode customerNode =
    new DefaultMutableTreeNode(customer);
    customerNode.add(new DefaultMutableTreeNode(
    customerNode.add(new DefaultMutableTreeNode(
    customerNode.add(new DefaultMutableTreeNode(
    treeModel = new DefaultTreeModel(rootNode);
    tree = new JTree(treeModel);

    public TreeCellRenderer getTreeCellRenderer() {
    return treeCellRenderer;

    private DefaultTreeCellRenderer treeCellRenderer =
    new FocusableTreeCellRenderer() {
    public Component getTreeCellRendererComponent(
    JTree tree, Object value, boolean sel,
    boolean expanded, boolean leaf, int row,
    boolean hasFocus) {
    tree, value, sel, expanded, leaf, row, hasFocus);
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
    if (node.isRoot()) {
    } else {
    Object userObject = node.getUserObject();
    if (userObject instanceof Customer) {
    Customer o = (Customer) userObject;
    this.setText(o.getFirstName() + " " + o.getLastName());
    } else {
    return this;

  3. Next, change the "CustomerView.createControl" as follows:
    protected JComponent createControl() {
    //No need to create the customer table anymore:
    //customerTable = new customerTableFactory().createCustomerTable();
    //Create the tree instead:
    JPanel view = new JPanel(new BorderLayout());
    //No need to add the JTable to the JScrollPane anymore:
    //JScrollPane sp = getComponentFactory().createScrollPane(customerTable.getControl());
    //Add the tree to the JScrollPane instead:
    JScrollPane sp = getComponentFactory().createScrollPane(tree);
    view.add(sp, BorderLayout.CENTER);
    return view;


Notice something interesting—or, in fact, boring—we've done a lot of coding to replace our AbstractObjectTable with a JTree. And none of what we've done is specific to Spring RCP—we've only used a standard Swing JTree, after all. However, we're displaying the same data as before. When we were dealing with the Spring RCP forms, how much coding did we need to do to replace one dialog with another? Just one line. We replaced the instantiation of the "TabbedDialogPage" with the instantiation of the "TreeCompositeDialogPage". Wouldn't it be cool if we could do the same thing for our data... and not just for our forms? That's what the next section is all about.


figure-1.png23.7 KB
figure-2.png27.1 KB
figure-3.png17.91 KB
figure-4.png20.39 KB
figure-5.png12.4 KB
figure-6.png36.5 KB
figure-7.png24.6 KB
figure-8.png43.48 KB
Published at DZone with permission of its author, Geertjan Wielenga.


Andreas Mueller replied on Fri, 2009/10/09 - 5:35am

I'm starting to learn Spring RCP and I really want to thank you for your great tutorials.

But I'm having problems to compile the code after introducing the HierarchicalFormModel.

The lines

customerForm = new CustomerForm(FormModelHelper.createChildPageFormModel(ownerFormModel, null));
addressForm = new AddressForm(FormModelHelper.createChildPageFormModel(ownerFormModel, null));

have errors and I have no idea how to change the constructors of CustomerForm and AddressForm to make it compile.

Did I miss the source code for this?


Andreas Mueller replied on Fri, 2009/10/09 - 5:47am

I did not expect it to be so easy.
I changed the constructor of AddressForm to

AddressForm(ValidatingFormModel model) {

 and of CustomerForm to

CustomerForm(ValidatingFormModel model) {

and that was it!


Andreas Mueller replied on Fri, 2009/10/09 - 6:07am

Obviously it is not as easy as I thought.

After making the abovementioned changes in the constructors the code compiles and the app runs, but changes in data are not saved to the model.

Any help available?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.