banner

JTable

Optional Reading

We will be using the Creating a GUI with JFC/Swing tutorial from Sun as a Reference source. You are not required to read the entire tutorial, I suggest you skim through it though to use a a supplement to what I give you here.

JTable

Our last major component that we will visit is the JTable. Like JList and JTree, JTable can be very simple, or you can do some really interesting stuff with it.

JTable displays data that is represented by the TableModel interface. We've seen before how these internal models can be very simple, or they can have their internal methods implement database calls, etc to "populate" the table. (I know of one case where the contents of table cells represented the results of system calls).

You can get an idea of how much can go into a table by looking at the below diagram of the different components that make up a JTable.

JTable components
JTable components

You can see that there is a great deal of mapping the Data in the table to the view of the table. This lets you have multiple views, hidden columns, all sorts of advanced features.

One feature in JTable that was new in Java SE version 5 (JDK 1.5) was the addition of an "upgraded" print() method. You can now print out the contents of a JTable just like you would any other spreadsheet. You can scale the table to fit on a single sheet, or you can print it across multiple sheets with the appropriate headers.

You can also provide your own custom renderers and even custom editors for a JTable letting you stray far away from just displaying mere numbers.

Java SE version 6 (JDK 1.6) added the ability to easily sort the table by clicking on different column headers. For most use, this is a single line of code

tbl.setRowSorter(new TableRowSorter(model));
where tbl is your JTable object
model is you TabelModel used with the JTable

You can make a table just by passing two arrays to the constructor

new JTable(Object[][] data, Object[] headers)

Which will take the objects and place them in a table, using the array of Objects provided as headers. As with any default renderers, icons will be rendered as icons, everything else has it's toString() method called.

To show how you can vary from the simplest case above, let's take the food example from the JList section and display the choices as a table instead of a list.

Download and open the Netbeans project jtable. When you run the application, you'll get a window that looks like this:

JTable

The significant part of this project is the class FoodModel. FoodModel extends AbsractTableModel. All we need to do is implement three methods:

int getRowCount()
int getColumnCount()
Object getValueAt(int row, int column)

You can see how that is done in the file. There are two more methods we need to override to get things to look as nice as it does above.

The first is the method getColumnName(int column). JTree queries this method to see what name to put in the headers, otherwise you just get A, B, C...

The second method is the getColumnClass(int column) method. Try commenting out this method and running the app. What happens? This is because the JTable renderer doesn't know that the items in column three are of ImageIcon class, so it just calls the toString() method on them to render them. If we implement this function and return the proper class, we get much nicer results.