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


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.

Example

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

Show Table Header

Hide Table Header

Updated on: 10-Feb-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements