Tables

Tables

The Swing model for tables is similar to the MVC approach of separation of presentation from data. It uses a JTable to control presentation and a TableModel to handle the data. This separation is a good thing, however, the mapping of data into a TableModel can be cumbersome and time consuming. Tamarin provides some assistance in the tamarin.table package. It provides the interfaces TableRow and TableRowTemplate to encapsulate the row data and column structure respectively. When combined with the RowTableModel this provides you with a simple and flexible method for constructing tables in Swing. Here we show the implementation of the template, row and table model for displaying Car objects. We’ll use this in our next example.

class CarRowTemplate implements TableRowTemplate {
        private static final int MAKE = 0;
        private static final int MODEL = 1;
        private static final int PRICE = 2; 

        public int getColumnCount() {
            return 3;
        } 

        public Class getColumnClass(int column) {
            switch (column) {
                case MAKE:
                    return String.class;
                case MODEL:
                    return String.class;
                case PRICE:
                    return Integer.class;
                default :
                    throw new IllegalArgumentException("No such column");
            }
        } 

        public String getColumnName(int column) {
            switch (column) {
                case MAKE:
                    return "Make";
                case MODEL:
                    return "Model";
                case PRICE:
                    return "Price (?)";
                default :
                    throw new IllegalArgumentException("No such column");
            }
        }
    } 

    class CarRow implements TableRow {
        private Car car; 

        public Object getValueAt(int column) {
            switch (column) {
                case CarRowTemplate.MAKE:
                    return car.getMake();
                case CarRowTemplate.MODEL:
                    return car.getModel();
                case CarRowTemplate.PRICE:
                    return car.getPrice();
                default :
                    throw new IllegalArgumentException("No such column");
            }
        } 

        public void setValueAt(int column, Object aValue) {
            switch (column) {
                case CarRowTemplate.MAKE:
                    car.setMake((String)aValue);
                case CarRowTemplate.MODEL:
                    return car.setModel((String)aValue);
                case CarRowTemplate.PRICE:
                    return car.setPrice((Integer)aValue);
                default :
                    throw new IllegalArgumentException("No such column");
            }
        }
    }
	 

    class CarTableModel extends RowTableModel {
        private static final int INITIAL_CAPACITY = 20;
        public CarTableModel() {
            super(new CarRowTemplate(), INITIAL_CAPACITY);
        }
    }