9

In order to have a Netbeans liked property inspector windows, I am making use of the following class to help me achieve this.

com.l2fprod.common.propertysheet.PropertySheetPanel

So far, it works fine for class with simple properties like String, int...

However, when come to slightly complicated class with composited relationship, things get more complicated.

For example, I have two animals (interface). One is Cat (Simple class with name and age) and Dog (Another simple class with name and age).

It takes no effort to display them through GUI windows.

However, when come to class with composited relationship. A Zoo, which can contains multiple animals (A class with array list to hold animals), I have problem to display all the animals properties within a single window.

The following is the screen shoot

alt text
(source: googlepages.com)

Partial source code is shown here

    ObjectInspectorJFrame objectInspectorJFrame0 = new ObjectInspectorJFrame(cat);
    objectInspectorJFrame0.setVisible(true);
    objectInspectorJFrame0.setState(java.awt.Frame.NORMAL);

    ObjectInspectorJFrame objectInspectorJFrame1 = new ObjectInspectorJFrame(dog);
    objectInspectorJFrame1.setVisible(true);
    objectInspectorJFrame1.setState(java.awt.Frame.NORMAL);

    // I wish to see all "animals" and their properties in this windows. :(
    // How?
    ObjectInspectorJFrame objectInspectorJFrame2 = new ObjectInspectorJFrame(zoo);
    objectInspectorJFrame2.setVisible(true);
    objectInspectorJFrame2.setState(java.awt.Frame.NORMAL);

Complete source code can be downloaded from

http://yancheng.cheok.googlepages.com/sandbox.zip

I wish within "Zoo" windows, it can display all the properties for all animals.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • I'm very interested in whether you found a solution to this problem. I have exactly the same issue – I82Much Dec 04 '09 at 15:20

1 Answers1

0

PropertySheetPanel as is only populates its table reading the properties for a given Java Bean.

You need to extend PropertySheetPanel behaviour and populate the properties from a given Collection. Iterate your collection and use addProperty(Property) to populate the table.

You can also use instrospection or beanutils lib to discover the collection elements.

EDIT: Example added.

package com.stackoverflow.swing.PropertySheetPanel;

import java.util.ArrayList;
import java.util.Collection;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import com.l2fprod.common.propertysheet.DefaultProperty;
import com.l2fprod.common.propertysheet.PropertySheetPanel;

/**
 * An example that creates a l2fprod PropertySheetPanel that displays any
 * Collection.
 */
public class CollectionPropertySheet<C> extends PropertySheetPanel {

    // Choose some bean. An animal as example.
    static class Animal {
        private String name;
        private String family;

        public Animal(String name, String family) {
            this.name = name;
            this.family = family;
        }

        @Override public String toString() {
            return name + " " + family;
        }
    }

    /**
     * @param simpleModel The input collection as data model.
     */
    public CollectionPropertySheet(Collection<C> simpleModel) {
        super();
        populateCollectionProperties(simpleModel);
    }

    private void populateCollectionProperties(Collection<C> collection) {
        int index = 0;
        for (C entry : collection) {
            // Define property properties 
            DefaultProperty property = new DefaultProperty();
            property.setDisplayName(entry.getClass().getSimpleName() + "[" + index++ +"]");
            property.setValue(entry.toString());
            // Set any other properties ... 
            // and add.
            addProperty(property);
        }
    }

    // Start me here!
    public static void main(String[] args) {
        // Inside EDT
        SwingUtilities.invokeLater(new Runnable() {
            @Override public void run() {
                JFrame frame = new JFrame("A simple example...");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new CollectionPropertySheet<Animal>(getAnimals()));
                frame.pack();
                frame.setVisible(true);
            }

            private Collection<Animal> getAnimals() {
                Collection<Animal> animals = new ArrayList<Animal>();
                animals.add(new Animal("Lion", "Felidae"));
                animals.add(new Animal("Duck", "Anatidae"));
                animals.add(new Animal("Cat", "Felidae"));
                return animals;
            }
        });
    }

}
hfernandes
  • 54
  • 4
  • This is example of class with primitive fields. However, I am looking for answer for class with composited field. Here is my solution for class with primitive fields. http://jstock.cvs.sourceforge.net/viewvc/jstock/jstock/src/org/yccheok/jstock/gui/ObjectInspectorJPanel.java?view=markup – Cheok Yan Cheng Jun 10 '09 at 04:02