草庐IT

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

coder 2024-03-08 原文

为了让 Netbeans 喜欢属性检查器窗口,我正在使用以下类来帮助我实现这一点。

com.l2fprod.common.propertysheet.PropertySheetPanel

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

但是,当涉及到具有复合关系的稍微复杂的类时,事情变得更加复杂。

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

通过 GUI 窗口毫不费力地显示它们。

但是,当以复合关系上课时。一个动物园,它可以包含多个动物(一个带有数组列表的类来保存动物),我无法在一个窗口中显示所有动物属性。

以下是截图


(来源:googlepages.com)

此处显示部分源代码

    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 仅填充其表以读取给定 Java Bean 的属性。

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

您还可以使用 instrospectionbeanutils 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;
            }
        });
    }

}

关于java - 有 com.l2fprod.common.propertysheet.PropertySheetPanel 来显示复合类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/848538/

有关java - 有 com.l2fprod.common.propertysheet.PropertySheetPanel 来显示复合类的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  4. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  5. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  6. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  7. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  8. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  9. java - 什么相当于 ruby​​ 的 rack 或 python 的 Java wsgi? - 2

    什么是ruby​​的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht

  10. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

随机推荐