Java JTable Tricks
Wednesday December 20 2017

Java's JTable is a GUI component that has been around for many many years. While a bit difficult to work with, it's almost a mandatory component in an application that displays a lot of data.

In this post I share some nice tricks for working with JTables in Java applications.

Scrolling

A JTable by itself does not provide any scrolling when handling a lot of data. To fix this, we need to place the table inside a JScrollPane component. Additionally, we can place this JScrollPane into a JPanel in our window:

File Type Cells

Let's say we want to associate a file to each record from the table. To do this, we can add a special column that will contain the File object, and to select this file we want to use the super useful JFileChooser component.

To achieve this, we need to set a custom cell editor to the column. Assuming we know the index of this special column, we can set it like this: