Java programming course: 18.1 Finishing off the User Interface


Finishing off the User Interface

This section puts the finishing touches to the graphical part of the application by creating a read-only table for the zoo's visitors, defining a menu-bar and toolbar, and by making the application adopt the running system's look-and-feel.

In this section you will learn:

  • How to develop a table of data
  • How to define a menu-bar
  • How to define a toolbar
  • How to adopt the running system's look-and-feel

A table of visitors

For simplicity, the list of visitors, as defined in ZooAdministrator, will be displayed in a read-only table in a tab of the AdministratorFrame. First, you need to do some preparatory work in the ZooAdministrator class:

You will define a collection for the visitors, so replace the following line: 

private Visitor mary, peter, richard, tanya; 

With this:

private Collection<Visitor> visitors; 

In the constructor you need to instantiate an empty collection, before the call to createExampleVisitors():

visitors = new TreeSet<Visitor>(); 

Define a method that returns the collection:

public Collection<Visitor> getVisitors() {
    return Collections.unmodifiableCollection(visitors);
}
 

Modify the createExampleVisitors() method to utilise the collection rather than the previously named variables:

private void createExampleVisitors() {
    visitors.add(new Visitor(new Person("Mary Roberts"),
                                 new Email("")));
    visitors.add(new Visitor(new Person("Peter Harris"),
                                 new Email("")));
    visitors.add(new Visitor(new Person("Richard York"),
                                 new Email("")));
    visitors.add(new Visitor(new Person("Tanya West"),
                                 new Email("")));
}
 

In virtualzoo.ui create a new class called VisitorTable which extends JTable:

package virtualzoo.ui;

import virtualzoo.core.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class VisitorTable extends JTable {
    
    private TableModel model;
    
    VisitorTable() {
        model = new VisitorTableModel();
        setModel(model);
    }
    
    
    private class VisitorTableModel extends AbstractTableModel {
        
        private List<Visitor> visitors;
        
        public VisitorTableModel() {
            ZooAdministrator admin = ZooAdministrator.getInstance();
            visitors = new ArrayList<Visitor>(admin.getVisitors());
        }

        @Override
        public int getRowCount() {
            return visitors.size();
        }

        @Override
        public int getColumnCount() {
            return 3;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            switch (columnIndex) {
                case 0:
                    // First column - vistor's name
                    return visitors.get(rowIndex).getName();
                case 1:
                    // Second column - visitor's email
                    return
                        visitors.get(rowIndex).getEmail().toString();
                case 2:
                    // Third column - visitors sponsored animal
                    Animal sponsoredAnimal =
                         visitors.get(rowIndex).getSponsoredAnimal();
                    if (sponsoredAnimal != null) {
                        return sponsoredAnimal;
                    } else {
                        return "";
                    }
                default:
                    // Should not happen
                    throw new IllegalStateException();
            }
        }

        @Override
        public String getColumnName(int columnIndex) {
            String[] columnHeaders =
                    {"Name", "Email Address", "Sponsored Animal"};
            return columnHeaders[columnIndex];
        }
    
    }
    
}
 

The above class draws on very similar techniques to those shown in Section 14 for developing a JTable and associated TableModel:

  • The class extends JTable and includes an inner class that extends AbstractTableModel to manage the actual data
  • The data is obtained from the ZooAdministrator into an ArrayList, and the getRowCount(), getColumnCount(), getValueAt() and getColumnName() methods are overridden to provide the various table values

Create a new tab in the constructor of AdministratorFrame:

// Create visitor table
VisitorTable visitorTable = new VisitorTable();
tabPane.addTab("Visitors", new JScrollPane(visitorTable));
 

The application should now look like this:

Visitors table


Print
×
Stay Informed

When you subscribe, we will send you an e-mail whenever there are new updates on the site.

Related Posts

 

Comments

No comments made yet. Be the first to submit a comment
Monday, 27 October 2025

Captcha Image