How can we show/hide the table header of a JTable in Java?

AWTJavaObject Oriented ProgrammingProgrammingSwing

A JTable is a subclass of JComponent class for displaying complex data structures. A JTable can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. The DefaultTableModel class is a subclass of AbstractTableModel and it can be used to add the rows and columns to a JTable dynamically. The DefaultTableCellRenderer class can extend JLabel class and it can be used to add images, colored text and etc. inside the JTable cell. 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.


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) {
      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());
      JCheckBox check = new JCheckBox("JTableHeader visible: ", true);
      check.addActionListener(ae -> {
         JCheckBox cb = (JCheckBox) ae.getSource();
      add(check, BorderLayout.NORTH);
   public static void main(String[] args) {
      JFrame frame = new JFrame("JTableHeaderHide Test");
      frame.setSize(375, 250);
      frame.getContentPane().add(new JTableHeaderHideTest());


Show Table Header

Hide Table Header

Updated on 10-Feb-2020 07:44:45