Creating a Java Table Using JTable

Child using a laptop
Sally Anscombe / Getty Images

Java provides a useful class called JTable that enables you to create tables when developing graphical user interfaces using the components of Java's Swing API. You can enable your users to edit the data or just view it. Note that the table doesn't actually contain data — it's entirely a display mechanism.

This step-by-step guide will show how to use the class

to create a simple table.

Note: Like any Swing GUI, you'll need to make a container in which to display the 

. If you're unsure how to do this then look at

.

Using Arrays to Store the Table Data

A simple way to provide data for the

class is to use two arrays. The first holds the column names in a

array:

The second array is a two-dimensional object array that holds the data for the table. This array, for example, includes six Olympic swimmers:

The key here is to make sure the two arrays have the same number of columns.

Constructing the JTable

Once you have the data in place, it's a simple task to create the table. Just call the

JTable
constructor and pass it the two arrays:You will probably want to add scroll bars to ensure the user can see all the data. To do so, place the
JTable
into a
JScrollPane
:Now when the table is displayed, you will see the columns and rows of data and will have the capability to scroll up and down.

The JTable object provides an interactive table. If you double-click on any of the cells, you will be able to edit the contents — although any editing affects only the GUI, not the underlying data. (An event listener would need to be implemented to handle the changing of data.).

To change the widths of the columns, hover the mouse on the edge of a column header and drag it back and forth. To change the order of the columns, click and hold a column header, then drag it to the new position.

Sorting Columns

To add the ability to sort the rows, call the

setAutoCreateRowSorter
method:When this method is set to true, you can click on a column header to sort the rows according to the contents of the cells under that column.

Changing the Appearance of the Table

To control the visibility of the grid lines, use the

setShowGrid
method:To change the color of the table altogether, use the
setBackground
and
setGridColor
methods:The column widths of the table are equal by default. If the container the table is in is re-sizeable, then the widths of the columns will expand and shrink and the container grows bigger or smaller. If a user resizes the column, then the width of columns to the right will change to accommodate the new column size.

The initial column widths can be set using the setPreferredWidth method or a column. Use the TableColumn class to first get a reference to the column, and then the setPreferredWidth method to set the size:

Selecting Rows

By default, the user can select the rows of the table in one of three ways:

  • To select a single row, select a table cell in that row.
  • To select continuous, multiple rows, drag the mouse over several rows or select the table cells with the shift cell pressed.
  • To select non-continuous, multiple rows, select table cells while holding down the control key (command key for Macs).

Using a Table Model

Using a couple of arrays for the data of a table can be useful if you want a simple String-based table which can be edited. If you look at the data array we created, it contains other data types than

- the

column contains

and the

column contains

. Yet both these columns are displayed as Strings. To change this behavior, create a table model.

A table model manages the data to be displayed in the table. To implement a table model, you can create a class that extends the

class:

The six methods above are those used in this step-by-step guide, but there are more methods defined by the

class that are useful in manipulating the data in a

object. When extending a class to use the

you are required to implement only the

,

and

methods.

Create a new class implementing those five methods shown above:

It makes sense in this example for the

class to hold the two strings containing the table data. Then, the

,

and

methods can use the arrays to provide the values for the table. Also, notice how the

method has been written to disallow the first two columns to be edited.

Now, instead of using the two arrays to create the

object, we can use the

class:

When the code runs, you will see that the

object is using the table model because none of the table cells are editable, and the column names are being correctly used. If the

method had not been implemented, then the column names on the table would display as the default names of A, B, C, D, etc.

Let's now consider the method 

. This alone makes the table model worth the implementation because it provides the

object with the data type contained within each column. If you remember, the object data array has two columns that aren't

data types: the

column which contains ints, and the

column which contains

. Knowing these data types changes the functionality provided by the

object for those columns. Running the sample table code with the table model implemented means the

column will actually be a series of checkboxes.

Adding a ComboBox Editor

You can define custom editors for the cells in the table. For example, you could make a combo box an alternative to the standard text editing for a field.

Here's an example using 

the country field:

To set the default editor for the country column, use the

class to get a reference to the country column, and the

method to set the

as the cell editor: