Show/Hide Table Header of JTable in Java



While using JTables in Java Swing, you may have cases where you would require displaying or hiding the column headers dynamically. In this article, we will learn how to show/hide the table header of a JTable in Java.

JTable

A JTable is a subclass of the JComponent class for displaying complex data structures. A JTable can follow the Model View Controller (MVC) design pattern to display the data in rows and columns.

DefaultTableModel

The DefaultTableModel class is a subclass of AbstractTableModel, and it can be used to add rows and columns to a JTable dynamically. The DefaultTableCellRenderer class can extend the JLabel class, and it can be used to add images, colored text, etc., inside the JTable cell.

Method Declaration:

public class DefaultTableModel extends AbstractTableModel

Showing/Hiding the Table Header of a JTable

We can hide the table header of a JTable by unchecking the JCheckBox, and show the table header of a JTable by clicking the JCheckBox.

The following are the steps for showing/hiding the table header of a JTable in Java:

Table Data

Declaring a string array named "columnNames" to define column headers and a 2D Object array, named "data", which contains data for the respective headers, and has 4 rows.

private final String[] columnNames = {"String", "Integer", "Boolean"};
private final Object[][] data = {
    {"Tutorials Point", 100, true},
    {"Tutorix", 200, false},
    {"Tutorials Point", 300, true},
    {"Tutorix", 400, false}
};

Custom Table Model

Constructs an anonymous inner class extending DefaultTableModel is constructed: getColumnClass() method is overridden so that column types are determined by looking at the class of the first row of data.

private final TableModel model = new DefaultTableModel(data, columnNames) {
    @Override
    public Class getColumnClass(int column) {
        return getValueAt(0, column).getClass();
    }
};

Components Creation

The "table" variable of the JTable component shows the tabular data, and the "scrollPane" is the JScrollPane that contains the table to provide scrolling.

private final JTable table = new JTable(model);
private final JScrollPane scrollPane = new JScrollPane(table);

JTableHeaderHideTest() Function

The JTableHeaderHideTest() function uses the JPanel constructor with a BorderLayout and adds the JScrollPane containing the JTable to the panel.

public JTableHeaderHideTest() {
    super(new BorderLayout());
    add(scrollPane);

Toggling the Checkbox

Creates a JCheckBox with the label "JTableHeader visible: ". To handle checkbox clicks, we will be using a lambda expression with an ActionListener, and the checkbox is made selected by default.

When you click it, it retrieves the checkbox state through the isSelected() method, hides or displays the header through the setVisible() method according to the selection state, and refreshes the GUI by using the revalidate() method to refresh the screen.

JCheckBox check = new JCheckBox("JTableHeader visible: ", true);
    check.addActionListener(ae -> {
        JCheckBox cb = (JCheckBox) ae.getSource();
        scrollPane.getColumnHeader().setVisible(cb.isSelected());
        scrollPane.revalidate();
    });
    add(check, BorderLayout.NORTH);
}

Example

Below is an example of showing/hiding the table header of a JTable in Java:

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public final class JTableHeaderHideTest extends JPanel {
   private final String[] columnNames = {"String", "Integer", "Boolean"};
   private final Object[][] data = {{"Tutorials Point", 100, true}, {"Tutorix", 200, false},
   {"Tutorials Point", 300, true}, {"Tutorix", 400, false}};
   private final TableModel model = new DefaultTableModel(data, columnNames) {
      @Override
      public Class getColumnClass(int column) {
         return getValueAt(0, column).getClass();
      }
   };
   private final JTable table = new JTable(model);
   private final JScrollPane scrollPane = new JScrollPane(table);
   public JTableHeaderHideTest() {
      super(new BorderLayout());
      add(scrollPane);
      JCheckBox check = new JCheckBox("JTableHeader visible: ", true);
      check.addActionListener(ae -> {
         JCheckBox cb = (JCheckBox) ae.getSource();
         scrollPane.getColumnHeader().setVisible(cb.isSelected());
         scrollPane.revalidate();
      });
      add(check, BorderLayout.NORTH);
   }
   public static void main(String[] args) {
      JFrame frame = new JFrame("JTableHeaderHide Test");
      frame.setSize(375, 250);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new JTableHeaderHideTest());
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
}

Output

The table below shows the table header visible to the user when the checkbox is selected:

The table below hides the table header for the user when the checkbox is not selected:

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-05-15T11:25:12+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements