Create/Register/Initialize JavaBeans in a NetBeans Platform Application

You can construct UI components from entries in the layer file, by using the Lookup to read a folder in the registry, and initializing values based on attributes returned via factory methods:
setLayout(new BorderLayout());

Car[] cars = Lookups.forPath("Cars").lookupAll(Car.class).toArray(new Car[0]);

JPanel panel = new JPanel(new MigLayout());

for (Car car : cars) {

    String type = car.getType();
    panel.add(new JLabel(type+":"));

    JTextField yearField = new JTextField();
    String year = String.valueOf(car.getYear());
    yearField.setText(year);
    panel.add(yearField, "wrap");

}

add(panel, BorderLayout.CENTER);

Result of the above:

And this is how you'd register your JavaBeans:

<folder name="Cars">
    <file name="org-car-HondaCar.instance">
        <attr name="instanceCreate" methodvalue="org.car.HondaCar.factory" />
        <attr name="type" stringvalue="Honda"/>
        <attr name="year" intvalue="1983"/>
        <attr name="position" intvalue="100"/>
    </file>
    <file name="org-car-MazdaCar.instance">
        <attr name="instanceCreate" methodvalue="org.car.MazdaCar.factory" />
        <attr name="type" stringvalue="Mazda"/>
        <attr name="year" intvalue="1979"/>
        <attr name="position" intvalue="200"/>
    </file>
</folder>

Each Car registered above implements this interface:

public interface Car {

    public String getType();
    public int getYear();
    
}

For example, this is HondaCar:

package org.car;

import java.util.Map;

public class HondaCar implements Car {

    String type;
    int year;

    @Override
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public HondaCar(String type, int year) {
        this.type = type;
        this.year = year;
    }

    public static Car factory(Map attrs) {
        String type = attrs.get("type").toString();
        int year = Integer.parseInt(attrs.get("year").toString());
        return new HondaCar(type, year);
    }
    
}

Related reading: http://forums.netbeans.org/post-33219.html

A next step is to create @Car.Registration annotation which you'd use on each of the Car implementations, instead of manually registering them in the layer file.

Comments:

Rather than copying to an array, a little cleaner to just:

for (Car car : Lookups.forPath("Cars").lookupAll(Car.class)) {...}

Note that in your example, HondaCar.factory in fact has nothing specific to Hondas in it, which makes the demo a bit confusing.

Little bit cleaner way to get attributes from an untyped map:

String type = (String) attrs.get("type");
int year = (Integer) attrs.get("year");

Posted by Jesse Glick on January 18, 2011 at 11:26 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
23
24
25
26
27
28
29
30
   
       
Today