有com.l2fprod.common.propertysheet.PropertySheetPanel显示复合类

Che*_*eng 9 java

为了拥有一个Netbeans喜欢的属性检查器窗口,我正在使用以下类来帮助我实现这一目标.

com.l2fprod.common.propertysheet.PropertySheetPanel

到目前为止,它适用于具有String,int等简单属性的类.

然而,当涉及复合关系的稍微复杂的类时,事情变得更加复杂.

例如,我有两只动物(界面).一个是Cat(名字和年龄的简单类)和Dog(另一个名字和年龄的简单类).

它不需要通过GUI窗口显示它们.

但是,来上课与合成关系.一个动物园,它可以包含多个动物(一个带有数组列表以容纳动物的类),我有问题在一个窗口内显示所有动物属性.

以下是屏幕截图

alt text http://yancheng.cheok.googlepages.com/object-inspector.png

部分源代码如下所示

    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);
Run Code Online (Sandbox Code Playgroud)

完整的源代码可以从中下载

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

我希望在"动物园"窗口内,它可以显示所有动物的所有属性.

hfe*_*des 0

PropertySheetPanel 仅填充其表,读取给定 Java Bean 的属性。

您需要扩展 PropertySheetPanel 行为并填充给定 Collection 中的属性。迭代您的集合并使用 addProperty(Property) 填充表。

您还可以使用introspectionbeanutils lib 来发现集合元素。

编辑:添加示例。

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;
            }
        });
    }

}
Run Code Online (Sandbox Code Playgroud)