Java 让com.l2fprod.common.propertysheet.PropertySheetPanel显示合成类
为了拥有一个类似Netbeans的属性检查器windows,我正在使用下面的类来帮助我实现这一点 com.l2fprod.common.propertysheet.PropertySheetPanel 到目前为止,它适用于具有简单属性的类,如字符串、int 然而,当涉及到具有复合关系的稍微复杂的类时,事情会变得更加复杂 例如,我有两个动物(界面)。一个是Cat(带有名字和年龄的简单类)和Dog(另一个带有名字和年龄的简单类) 通过GUI窗口显示它们不费吹灰之力 然而,当我们来到具有复合关系的课堂上时。一个动物园,它可以包含多个动物(一个带有数组列表的类可以容纳动物),我在单个窗口中显示所有动物属性时遇到了问题 以下是屏幕截图Java 让com.l2fprod.common.propertysheet.PropertySheetPanel显示合成类,java,Java,为了拥有一个类似Netbeans的属性检查器windows,我正在使用下面的类来帮助我实现这一点 com.l2fprod.common.propertysheet.PropertySheetPanel 到目前为止,它适用于具有简单属性的类,如字符串、int 然而,当涉及到具有复合关系的稍微复杂的类时,事情会变得更加复杂 例如,我有两个动物(界面)。一个是Cat(带有名字和年龄的简单类)和Dog(另一个带有名字和年龄的简单类) 通过GUI窗口显示它们不费吹灰之力 然而,当我们来到具有复合关系的课堂
(来源:) 部分源代码显示在这里
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);
完整的源代码可以从
我希望在“Zoo”窗口中,它可以显示所有动物的所有属性。PropertySheetPanel as只填充它的表,读取给定Java Bean的属性 您需要扩展PropertySheetPanel行为并从给定集合填充属性。迭代集合并使用addProperty(Property)填充表 您还可以使用或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;
}
});
}
}
package com.stackoverflow.swing.PropertySheetPanel;
导入java.util.ArrayList;
导入java.util.Collection;
导入javax.swing.JFrame;
导入javax.swing.SwingUtilities;
导入com.l2fprod.common.propertysheet.DefaultProperty;
导入com.l2fprod.common.propertysheet.PropertySheetPanel;
/**
*创建l2fprod PropertySheetPanel以显示任何
*收藏。
*/
公共类CollectionPropertySheet扩展PropertySheetPanel{
//选择一些豆子,例如动物。
静态类动物{
私有字符串名称;
私弦家庭;
公共动物(字符串名称、字符串族){
this.name=名称;
这个家庭=家庭;
}
@重写公共字符串toString(){
返回名称+“”+族;
}
}
/**
*@param simpleModel将输入集合建模为数据模型。
*/
公共集合属性表(集合simpleModel){
超级();
populateCollectionProperties(simpleModel);
}
私有void populateCollectionProperties(集合集合){
int指数=0;
对于(C条目:集合){
//定义属性属性
DefaultProperty=新的DefaultProperty();
setDisplayName(entry.getClass().getSimpleName()+“[”+索引+++“]);
property.setValue(entry.toString());
//设置任何其他属性。。。
//并添加。
不动产(不动产);
}
}
//从这里开始!
公共静态void main(字符串[]args){
//内EDT
SwingUtilities.invokeLater(新的Runnable(){
@重写公共无效运行(){
JFrame=newjframe(“一个简单的例子…”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(新的CollectionPropertySheet(getAnimals());
frame.pack();
frame.setVisible(true);
}
私人收藏动物(){
收集动物=新建ArrayList();
添加(新动物(“狮子”、“猫科”);
添加(新动物(“鸭”、“鸭科”);
添加(新动物(“猫”、“猫科”);
归还动物;
}
});
}
}
这是带有基本字段的类的示例。然而,我正在寻找复合字段类的答案。这是我对带有基本字段的类的解决方案。我很感兴趣的是你是否找到了解决这个问题的办法。我有完全相同的问题