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:
With this:
In the constructor you need to instantiate an empty collection, before the call to createExampleVisitors():
Define a method that returns the collection:
Modify the createExampleVisitors() method to utilise the collection rather than the previously named variables:
In virtualzoo.ui create a new class called VisitorTable which extends JTable:
The above class draws on very similar techniques to those shown in Section 14 for developing a JTable and associated TableModel:
- The class extends
JTableand includes an inner class that extendsAbstractTableModelto manage the actual data - The data is obtained from the
ZooAdministratorinto anArrayList, and thegetRowCount(),getColumnCount(),getValueAt()andgetColumnName()methods are overridden to provide the various table values
Create a new tab in the constructor of AdministratorFrame:
The application should now look like this: