Have com.l2fprod.common.propertysheet.PropertySheetPanel To Display Composited Class

前端 未结 1 1368
猫巷女王i
猫巷女王i 2021-01-02 09:56

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.Proper

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 10:29

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

    0 讨论(0)
提交回复
热议问题