Geertjan is a DZone Zone Leader and has posted 453 posts at DZone. You can read more from them at their website. View Full User Profile

How to Create a Drop-Down List in an OutlineView

05.02.2010
| 16996 views |
  • submit to reddit

Let's create this, i.e., a grid component (a NetBeans Platform OutlineView) that contains a JComboBox, creating something that looks like an Excel spreadsheet in your application:

Note: Automatically, because the "City" column is a property, the Properties window ALSO shows a JComboBox for the City property.

To create the above, read about NetBeans Inplace Editors in the NetBeans Property Editor Tutorial, as well as the Javadoc, such as the org.openide.explorer.propertysheet.InplaceEditor Javadoc.

Armed with the knowledge acquired from the above references, you are ready to read and use the code below to create a JComboBox in your OutlineView:

private class DemoChildFactory extends ChildFactory<Customer> {

    @Override
    protected boolean createKeys(List<Customer> list) {
        list.add(new Customer());
        list.add(new Customer());
        list.add(new Customer());
        return true;
    }

    @Override
    protected Node createNodeForKey(Customer key) {
        return new DemoNode(key);
    }

}

public class DemoNode extends AbstractNode {

    private DemoNode(Customer key) {
        super(Children.LEAF, Lookups.singleton(key));
        setDisplayName(key.getName());
    }

    @Override
    protected Sheet createSheet() {
        Sheet sheet = Sheet.createDefault();
        Sheet.Set set = Sheet.createPropertiesSet();
        Customer obj = getLookup().lookup(Customer.class);
        CityProperty cityProperty = new CityProperty(obj);
        set.put(cityProperty);
        sheet.put(set);
        return sheet;
    }

}

public class CityProperty extends PropertySupport.ReadWrite<String> {

    Customer c;

    public CityProperty(Customer c) {
        super("city", String.class, "City", "Name of City");
        this.c = c;
    }

    public String getValue() throws IllegalAccessException, InvocationTargetException {
        return c.getCity();
    }

    @Override
    public PropertyEditor getPropertyEditor() {
        return new CityPropertyEditorSupport(c);
    }

    public void setValue(String newValue) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        c.setCity(newValue);
    }

}

private class CityPropertyEditorSupport extends PropertyEditorSupport implements ExPropertyEditor, InplaceEditor.Factory {

    private final Customer c;

    private CityPropertyEditorSupport(Customer c) {
        this.c = c;
    }

    public String getAsText() {
        String s = (String) getValue();
        if (s == null) {
            return "No City Set";
        }
        return s;
    }

    public void setAsText(String s) {
        setValue(s);
    }

    public void attachEnv(PropertyEnv env) {
        env.registerInplaceEditorFactory(this);
    }

    private InplaceEditor ed = null;

    public InplaceEditor getInplaceEditor() {
        if (ed == null) {
            ed = new Inplace(c);
        }
        return ed;
    }

}

public class Inplace implements InplaceEditor {

    private final JComboBox comboBox = new JComboBox();
    private PropertyEditor editor = null;
    private final Customer c;

    private Inplace(final Customer c) {
        this.c = c;
        comboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                c.setCity(comboBox.getModel().getSelectedItem().toString());
            }
        });
    }

    public void connect(PropertyEditor propertyEditor, PropertyEnv env) {
        editor = propertyEditor;
        reset();
    }

    public JComponent getComponent() {
        return comboBox;
    }

    public void clear() {
        editor = null;
        model = null;
    }

    public Object getValue() {
        comboBox.repaint();
        comboBox.updateUI();
        ((JComponent) comboBox.getParent()).requestFocus();
        updateUI();
        repaint();
        return comboBox.getSelectedItem();
    }

    public void setValue(Object object) {
        comboBox.setSelectedItem(object);
        comboBox.repaint();
        comboBox.updateUI();
        ((Customer) object).setCity(comboBox.getSelectedItem().toString());
        ((JComponent) comboBox.getParent()).requestFocus();
    }

    public boolean supportsTextEntry() {
        return true;
    }

    public void reset() {
        comboBox.addItem("New York");
        comboBox.addItem("London");
        comboBox.addItem("Bergen");
        comboBox.addItem("Larryville");
        comboBox.addItem("Oslo");
        String str = (String) editor.getValue();
        if (str != null) {
            comboBox.setSelectedItem(str);

        }
    }

    public KeyStroke[] getKeyStrokes() {
        return new KeyStroke[0];
    }

    public PropertyEditor getPropertyEditor() {
        return editor;
    }

    public PropertyModel getPropertyModel() {
        return model;
    }
    private PropertyModel model;

    public void setPropertyModel(PropertyModel propertyModel) {
        this.model = propertyModel;
    }

    public boolean isKnownComponent(Component component) {
        return component == comboBox || comboBox.isAncestorOf(component);
    }

    public void addActionListener(ActionListener actionListener) {
        //do nothing - not needed for this component
    }

    public void removeActionListener(ActionListener actionListener) {
        //do nothing - not needed for this component
    }

}
 

And here's the Customer object referred to in the code above:

public class Customer {

private static int count = 0;
private final String name = "Person-" + count++;
private String city = "";
private PropertyChangeSupport propertyChangeSupport;

public Customer() {
count++;
propertyChangeSupport = new PropertyChangeSupport(this);
}

public void setCity(String d) {
String oldCity = city;
city = d;
propertyChangeSupport.firePropertyChange("city", oldCity, city);
}

public String getCity() {
return city;
}

public String getName() {
return name;
}

public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}

}

 

Published at DZone with permission of its author, Geertjan Wielenga.

Comments

John Kost replied on Tue, 2011/12/20 - 4:43pm

Hi Geertjan,

and thanks for this nice article. But, why the combo box doesn't appear if you change the DemoNode to extend BeanNode<Customer> instead of AbstractNode?

<code>

class DemoNode extends BeanNode<Customer> {

    DemoNode(Customer key) throws IntrospectionException {
        super(key, Children.LEAF, Lookups.singleton(key));
        setDisplayName(key.getName());
    }

</code>

 

James Marble replied on Fri, 2013/01/25 - 1:38pm

Is this example outdated? The documentation for InplaceEditor states:

"In no cases should an instance of InplaceEditor attempt to directly update the value ... ensure that getValue() will return the correct value, and fire the action command COMMAND_SUCCESS"

Tim Boudreau replied on Mon, 2013/02/04 - 9:30pm

Using a vanilla ComboBox in the property sheet is a recipe for all sorts of trouble, due to the way JTables and JComboBoxes interact (problems with the popup not appearing, problems with it being left behind, etc., with different problems on different look-and-feels).

For any case where you don't want custom cell-rendering for a JComboBox, instead please use either a plain PropertyEditor with getTags() overridden (which will be rendered as a JComboBox, just one with the necessary hacks to make it play nicely), or use one of the hints to provide string values to the integer property editor described here:  http://bits.netbeans.org/dev/javadoc/org-openide-explorer/org/openide/explorer/doc-files/propertyViewCustomization.html


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.