Hide a column in a JTable: just set column and header width to zero ;-) - Java Swing

Java examples for Swing:JTable Column

Description

Hide a column in a JTable: just set column and header width to zero ;-)

Demo Code


//package com.java2s;

import javax.swing.JTable;

import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class Main {
    /**//from  w  w  w  .  j  a  v a2s  . c  o m
     * Hide a column in a table: just set column and header width to zero ;-)
     *
     * @param table
     * @param column
     */
    public static void hideColumn(JTable table, int column) {

        TableColumnModel columnModel = table.getColumnModel();
        TableColumnModel headerColumnModel = table.getTableHeader()
                .getColumnModel();

        TableColumn tableColumn = columnModel.getColumn(column);
        TableColumn headerColumn = headerColumnModel.getColumn(column);

        // Set column width to zero
        tableColumn.setMaxWidth(0);
        tableColumn.setMinWidth(0);

        // Set column header width (to avoid the ... sign)
        headerColumn.setMaxWidth(0);
        headerColumn.setMinWidth(0);
    }
}

Related Tutorials