Java Swing GUI

How to Hide a Column in JTable

In this tutorial, we are going to see how to hide a column in JTable. JTable is a flexible Swing component that is very well suited to display data in a tabular format. In this tutorial, you will learn some techniques to hide a column in a JTable or to make a column in a JTable invisible.

In the example below, we have three columns “Name, address, ID”, and we have hidden the last column “ID”. In the console we print the hidden data using TableModel.


Method 1 :

You can use removeColumn() method.

removeColumn(TableColumn tcol)

This method is used to delete a column from JTable. It takes a TableColumn object that represents all attributes of a column in a JTable. tcol: It indicates the TableColumn that will be deleted.

//remove the second column

To get data from the model:


One thing to note is, when retrieving data, it must be extracted from the model and not from the table.


Method 2 :

You can set the column width to 0, so that it is invisible, for this we will use the following methods.

  • setMinWidth()
  • setMaxWidth()
  • setWidth()




Method 3 :

To hide a column (or more) in a JTable, do not give the column name. To get the hidden data, you must use the TableModel.


import javax.swing.*;
import java.awt.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.awt.event.*;

public class HideColumnJTable implements ListSelectionListener 
  JTable table;
  public HideColumnJTable() 
    JFrame f = new JFrame("Hide a column from JTable");

    // We only specify 2 columns, the last "ID" is hidden
    String[] columns = new String[] {

    Object[][] data = new Object[][] {
          {"Thomas", "Alaska", 1 },
          {"Jean", "Arizona", 2 },
          {"Yohan", "California", 3 },
          {"Emily", "Florida", 4 },
          {"Alex", "Boston", 5 },
    TableModel model = new AbstractTableModel() 
      public int getColumnCount() { 
        return columns.length; 
      public int getRowCount() { 
        return data.length;
      public Object getValueAt(int row, int col) {
        return data[row][col];
      public String getColumnName(int column) {
        return columns[column];
      public Class getColumnClass(int col) {
        return getValueAt(0,col).getClass();
      public void setValueAt(Object aValue, int row, int column) {
        data[row][column] = aValue;

    table = new JTable(model);

    ListSelectionModel listModel = table.getSelectionModel();

    JScrollPane scroll = new JScrollPane(table);
    scroll.setPreferredSize(new Dimension(300, 300));
    f.setSize(200, 180);

  public void valueChanged(ListSelectionEvent e) {
    int[] sel;
    Object value;

    if (!e.getValueIsAdjusting()) 
      sel = table.getSelectedRows();

      if (sel.length > 0) 
        for (int i=0; i < 3; i++) {
          // get data from the table
          TableModel tm = table.getModel();
          value = tm.getValueAt(sel[0],i);
          System.out.print(value + " ");

  public static void main(String[] args) {
    new HideColumnJTable();


mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *