- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- Can we hide the table header from a JTable in Java?
- How can we show/hide the echo character of a JPasswordField in Java?\n
- How can we filter a JTable in Java?
- How can we implement the word wrap JTableHeader of a JTable in Java?
- How can we prevent the re-ordering columns of a JTable in Java?
- How can we disable the cell editing inside a JTable in Java?
- How can we detect the double click events of a JTable row in Java?
- How can we remove a selected row from a JTable in Java?
- How can we sort a JTable on a particular column in Java?
- How to change JTable's header font in Java
- How can we add/insert a JButton to JTable cell in Java?
- How can we hide left/right pane of a JSplitPane programmatically in Java?
- How can we add/insert a JCheckBox inside a JTable cell in Java?
- How can we set the background/foreground color for individual column of a JTable in Java?
- How can we add/insert a JRadioButton to a JTable cell in Java?\n

Advertisements