DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Build a Simple Chat Server With gRPC in .Net Core
  • Test Data Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Payments Architecture - An Introduction
  • What Is Test Pyramid: Getting Started With Test Automation Pyramid

Trending

  • Build a Simple Chat Server With gRPC in .Net Core
  • Test Data Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Payments Architecture - An Introduction
  • What Is Test Pyramid: Getting Started With Test Automation Pyramid
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Create a Drop-Down List in an OutlineView

How to Create a Drop-Down List in an OutlineView

Geertjan Wielenga user avatar by
Geertjan Wielenga
·
May. 02, 10 · News
Like (0)
Save
Tweet
Share
35.65K Views

Join the DZone community and get the full member experience.

Join For Free

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

}

 

Property (programming) NetBeans Javadoc application Column (database) Object (computer science)

Opinions expressed by DZone contributors are their own.

Trending

  • Build a Simple Chat Server With gRPC in .Net Core
  • Test Data Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Payments Architecture - An Introduction
  • What Is Test Pyramid: Getting Started With Test Automation Pyramid

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: