Creation of Swing User Interface : Tables (JTable)

1. Introduction

This article will introduce you to create tables in Swing. This composent often gives problems when we start with Swing. I will try to explain the different concepts linked to the use of tables in Swing.
In this document we will see the basic concepts of JTable, the definition of table’s models, the dynamic modification of table, the way to edit the rendering of cells, the edition of the content of the table and finally the sort and the filter of the table.

2. Elementary concepts

A JTable is a Swing component enabling the program to display a table formed of a certain number of lines and columns. More than the content lines, the JTable has also a header line displaying a title for each column.

A JTable has data and header. We can see the data like a two-dimensional array in which all data correspond to the data of a cell and we can see the header’s data like a unidimentional array of String.
JTable use diffent concepts of Swing :

  • A model to keep the data. A JTable use a class implementing TableModel. We will see later how to specify a data model.
  • A renderer for the rendering of cells. We can specify a TableCellRenderer for each column class. Once again we will see that later.
  • An editor to edit the content of a cell. We can specify a TableCellEditor for each column class.

During this article, we will develop a very simple program to manage a list of friends. Here are the infromations about a friend :

  • A name and firnstname (Class String)
  • A favourite color (Class Color)
  • A gender (boolean man/woman)
  • A sport he like to do (Enum Sport)

Here is the Sport enumeration :

public enum Sport {
    TENNIS,
    FOOTBALL,
    SWIMMING,
    NOTHING
}

We will start with a basic first version of our application.

The easiest way, but not the best, is to use two arrays and to give them to the JTable constructor.

So, here is the most basic implementation of our program :

public class JTableBasicWithPanel extends JFrame {
    public JTableBasicWithPanel() {
        super();

        setTitle("Basic JTable in a JPanel");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Object[][] data = {
                {"Johnathan", "Sykes", Color.red, true, Sport.TENNIS},
                {"Nicolas", "Van de Kampf", Color.black, true, Sport.FOOTBALL},
                {"Damien", "Cuthbert", Color.cyan, true, Sport.NOTHING},
                {"Corinne", "Valance", Color.blue, false, Sport.SWIMMING},
                {"Emilie", "Schrödinger", Color.magenta, false, Sport.FOOTBALL},
                {"Delphine", "Duke", Color.yellow, false, Sport.TENNIS},
                {"Eric", "Trump", Color.pink, true, Sport.FOOTBALL},
        };

        String[] headers = {"First name", "Name", "Favourite color", "Gender", "Sport"};

        JTable table = new JTable(data, headers);

        getContentPane().add(table.getTableHeader(), BorderLayout.NORTH);
        getContentPane().add(table, BorderLayout.CENTER);

        pack();
    }

    public static void main(String[] args) {
        new JTableBasicWithPanel().setVisible(true);
    }

We use the constructor JTable(Object[][] data, Object[] entetes) to manage our data and headers. To add our table to a JPanel, we must add separately the header and the table itself.

That will give us this result :

JTable Basic With JPanel

JTable Basic With JPanel

With a few number of lines of code, we have a functional table. Nevertheless, this first implementation has some disadvantages :

  1. We cannot display more lines than the lines that can ben displayed by the window.
  2. The data are totally static
  3. We cannot manage the way the data are rendered.
  4. There is no distinction between the view and the datas
  5. The column Color and Gender are not really esthetic

The first point is easily solvable. The good way to add a JTable in container is to use a JScrollPane who enable to display more lines than the windows has space. For the rest of the article, we will always use a JScrollPane. So, here is a new version with a JScrollPane :

public class JTableBasicWithScrollPane extends JFrame {
    public JTableBasicWithScrollPane() {
        super();

        setTitle("Basic JTable in a JScrollPane");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Object[][] data = {
                {"Johnathan", "Sykes", Color.red, true, Sport.TENNIS},
                {"Nicolas", "Van de Kampf", Color.black, true, Sport.FOOTBALL},
                {"Damien", "Cuthbert", Color.cyan, true, Sport.NOTHING},
                {"Corinne", "Valance", Color.blue, false, Sport.SWIMMING},
                {"Emilie", "Schrödinger", Color.magenta, false, Sport.FOOTBALL},
                {"Delphine", "Duke", Color.yellow, false, Sport.TENNIS},
                {"Eric", "Trump", Color.pink, true, Sport.FOOTBALL},
        };

        String[] headers = {"First name", "Name", "Favourite color", "Gender", "Sport"};

        JTable table = new JTable(data, headers);

        getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);

        pack();
    }

    public static void main(String[] args) {
        new JTableBasicWithScrollPane().setVisible(true);
    }

Now, we add directly the entire JTable in the JScrollPane. Here is the result :

JTable Basic With ScrollPane

JTable Basic With ScrollPane

This version is already better than the previous, but this is not the best approach. We will improve it in the next chapters.

 

  • http://www.learn-blog.info Gregory Despain

    Great post thx!

  • http://www.learn-blog.info Gregory Despain

    Great post thx!

  • http://www.perfectoptimizerreview.net/ perfect optimizer best

    Which means that useful! Information.

  • http://www.perfectoptimizerreview.net/ perfect optimizer best

    Which means that useful! Information.

  • http://www.aryol.com.tr Prefabrik

    another great informative post. I’m glad to read it. Thanks a lot.