By Tony Bevis on Friday, 20 October 2023
Category: Java

Java programming course: 14.12 JTable

JTable

Whereas the JList class displayed a list of items in a single column, the JTable class is capable of showing items that each have several columns. You need to attach a TableModel object (which references the item data) to the JTable. The most common way of doing this is to subclass the AbstractTableModel class and override three methods that provide the total number of rows, the total number of columns, and the object at a particular row/column intersection, or "cell".

Continuing the simple examples from earlier, where you are using the strings "Red", "Green" and "Blue", assume you want a three-column table where the first column will be the normal text of the colour, the second column the text in upper-case, and the third column the number of characters in the word. Define the following inner class inside DemoUI, and note that you need to import package java.swing.table

The class ColourTableModel extends the Java supplied AbstractTableModel

The constructor is identical to that used for ColourListModel – it adds the three strings to an ArrayList object

The getRowCount() method returns the total number of rows in the table, which will correspond to the total number of items in the ArrayList object

The getColumnCount() method returns the total number of columns in the table, which in this case is three

The getValueAt() method provides two arguments, being the row index and the column index, where each of them starts from zero. You can think of a table as being like a spreadsheet, where the row and column indexes together point to a particular cell. The method does the following:


Now in the buildUI() method of the main body of DemoUI you can instantiate a ColourTableModel object, assign it to a new JTable object, put the table in a scroll pane and place it on the panel:

The JTable object takes care of invoking the appropriate ColourTableModel methods for you automatically. You should see the following table displayed:

There are a couple of points to note about the table as it stands:

  1. Because you have not specifically set the column headings it has defaulted to spreadsheet-style names of A, B, C etc.
  2. All items of data are left-aligned. While this is a sensible default for strings, numbers generally look better when right aligned

To set the column headers you can override the getColumnName() method inside the ColourTableModel inner class:

To make numbers right-aligned you can take advantage of the fact that tables are capable of using default alignments based upon the class of the Object which is returned by getValueAt(). To do this, override the getColumnClass() method inside ColourTableModel:


The table should now look as follows:

If you would like users to be able to sort the data on any of the columns, you can add the following statement after you instantiate the JTable object:

The user can now click on any column header to sort the table by that column. Each time a column is clicked it toggles between being sorted in ascending and descending sequence. A small arrow appears in the column heading to indicate the sequencing.

It's possible to enable table cells to be directly edited by the user. Suppose, for example, you want to enable the text of the colour to be edited in the first column. Because the values displayed in columns two and three depend on the text in column one then only column one should be editable. First, you need to override the isCellEditable() method inside ColourTableModel:


If you run the application, you can now double-click inside any entry in the first column to make the cell go into edit mode:

Try changing the text to something else and press Enter on your keyboard. You will notice the text revert to what it was previously! To save the edited change you need to override the setValue() method as well:


If you run the application again your change is made but you will notice that the upper-case version and number of characters in the second and third columns have not been updated. You can fix this by adding one more statement to the above method to tell the table that the data has been modified so that it can perform a refresh:

Next lesson: 14.13 JTree

Related Posts

Leave Comments