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
JTable
into a
JScrollPane

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

Changing the Appearance of the Table

To control the visibility of the grid lines, use the

setShowGrid
setBackground
and
setGridColor

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:

Format
mla apa chicago
Your Citation
Leahy, Paul. "Creating a Java Table Using JTable." ThoughtCo, Aug. 27, 2020, thoughtco.com/how-to-create-a-simple-table-2033894. Leahy, Paul. (2020, August 27). Creating a Java Table Using JTable. Retrieved from https://www.thoughtco.com/how-to-create-a-simple-table-2033894 Leahy, Paul. "Creating a Java Table Using JTable." ThoughtCo. https://www.thoughtco.com/how-to-create-a-simple-table-2033894 (accessed March 28, 2024).