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

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

com.l2fprod.common.propertysheet.PropertySheetPanel

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

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

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

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

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

以下是屏幕截图

alt text http://sofzh.miximages.com/java/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); 

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

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

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

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

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

您还可以使用instrospection或beanutils 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 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 simpleModel) { super(); populateCollectionProperties(simpleModel); } private void populateCollectionProperties(Collection 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(getAnimals())); frame.pack(); frame.setVisible(true); } private Collection getAnimals() { Collection animals = new ArrayList(); animals.add(new Animal("Lion", "Felidae")); animals.add(new Animal("Duck", "Anatidae")); animals.add(new Animal("Cat", "Felidae")); return animals; } }); } }