fuin.org

Small Open Source Java Tools and Libraries

TableFormatCreator

Create glazedlists External link table format "on-the-fly" from annotations in business object class.
 
Example business object:
public class Address {

    @TableColumn(width = 100, pos = 0)
    private String lastName = "";

    @TableColumn(width = 100, pos = 1)
    private String firstName = "";

    @TableColumn(width = 130, pos = 2)
    private String street = "";

    @TableColumn(width = 50, pos = 3, getter="getZipCode")
    private String zip = "";

    @TableColumn(width = 50, pos = 4)
    private String city = "";

    public Address() {
        super();
    }

    public String getLastName() {
        return lastName;
    }

    public Address setLastName(String lastName) {
        this.lastName = lastName;
        return this;
    }

    public String getFirstName() {
        return firstName;
    }

    public Address setFirstName(String firstName) {
        this.firstName = firstName;
        return this;
    }

    public String getStreet() {
        return street;
    }

    public Address setStreet(String street) {
        this.street = street;
        return this;
    }

    public String getZipCode() {
        return zip;
    }

    public Address setZipCode(String zip) {
        this.zip = zip;
        return this;
    }

    public String getCity() {
        return city;
    }

    public Address setCity(String city) {
        this.city = city;
        return this;
    }

}

Creating the table:
// Example table
JTable tableAdr = new JTable();
tableAdr.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

// List with addresses
EventList<Address> addresses = new BasicEventList<Address>();

// Create the table format "on-the-fly" from annotations in class "Address"
TableFormatCreator<Address> creator = new TableFormatCreator<Address>();
TableFormatExt tableFormat = creator.create(Address.class, Locale.ENGLISH);

// Set table model
tableAdr.setModel(new EventTableModel<Address>(addresses, tableFormat));

// Also set predefined width from annotations
TableColumnModel colModel = tableAdr.getColumnModel();
for (int i = 0; i < tableFormat.getColumnCount(); i++) {
    final TableColumn col = colModel.getColumn(i);
    col.setPreferredWidth(tableFormat.getColumnWidth(i));
}

Address_en.properties:
lastName=Last name
firstName=First name
street=Street
zip=ZIP-Code
city=City
Screenshot:
Example address table