SourceForge.net Logo Tamarin logo Tamarin monkey pic


What is Tamarin?

Tamarin is a framework to facilitate rapid development of Java Swing applications. It provides reusable components that drastically reduce the amount of coding required for creating and laying out Swing components.

Components of Tamarin

Layout

The core functionality of Tamarin is concerned with layouts. The tamarin.layout package provides several re-usable panels that allow you to add and lay out components easily.

Here's an example using the LabelledItemPanel...

    // Create a new panel with 2 columns
    LabelledItemPanel content = new LabelledItemPanel("Simple Layout Example", 2);
    content.addItem("First Name : ", new JTextField(20));
    content.addItem("Surname : ", new JTextField(20));
    content.addItem("Address : ", new JTextField(30));
    content.addItem("Telephone No : ", new JTextField(15));
    content.addItem("Password : ", new JPasswordField(10));
    content.addButton(new JButton("Save"));
    getContentPane().add(content); 

This example creates a panel with the 5 components arranged over two columns along with a Save button. For standard data entry we will probably want a Save and Cancel buttons. These are provided by the EntryForm class. For searching the SearchForm provides Search and Reset buttons, the user simply has to provide the associated search Action as seen below...

    // Create a new search form with 1 column 
    SearchForm form = new SearchForm("Search Form Example", 1, mySearchAction);
    form.addItem("Make : ", new JTextField(20));
    form.addItem("Model : ", new JTextField(25));
    form.addItem("Minimum Price : ", new JTextField(15));
    form.addItem("Maximum Price : ", new JTextField(15));
    getContentPane().add(form);

Resetable Components

If you use components in a search form then you will typically want to be able to reset the values in the criteria boxes to some default value. Typically this would be some default value for a combo box or blank for a text field. To simplify this Tamarin provides Resetable Components. A Resetable Component is a JComponent that implements the Resetable interface. To implement this the component implements the reset() method. On a search form, the Reset button then calls the reset methods of these components. In this example, when the user clicks Reset, all the text fields are reset to empty and the combo box defaults to the first item, 2.

	 
    Object[] doorItems = new String[]{"2", "3", "4", "5"}; 

    // Create a new search form with 1 column 
    SearchForm form = new SearchForm("Resetable Component Example", 1, mySearchAction); 
    form.addResetableItem("Make : ", new ResetableTextField(20)); 
    form.addResetableItem("Model : ", new ResetableTextField(25)); 
    form.addResetableItem("Minimum Price : ", new ResetableTextField(15)); 
    form.addResetableItem("Maximum Price : ", new ResetableTextField(15)); 
    form.addResetableItem("No of doors : ", new ResetableCombo(doorItems)); 
    getContentPane().add(form);

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

ResultsTablePanel

The ResultsTablePanel is a panel that you can use to display results in table format. It allows you to specify a table containing the results as well as providing add method to allow you to add components, such as text fields or buttons. For many situations you will want a table displaying some results, along with Add, Edit and Delete buttons. In this case use the FixedResultsTablePanel as this provides all these buttons as standard. All you have to do is provide the table and associated Add, Edit and Delete actions. Here we construct a ResultsTablePanel using our table of Car objects.

    JTable cars = new JTable(new CarTableModel()); 
    ResultsTablePanel carResults = new ResultsTablePanel("Table Example"); 
    carResults.setResultsTable(cars); 
    getContentPane().add(carResults);

Installing Tamarin

This part is really simple. All you do is download tamarin.jar and stick it in the classpath of your project.