show JTable Columns - Java Swing

Java examples for Swing:JTable Column

Description

show JTable Columns

Demo Code


//package com.java2s;

import javax.swing.JTable;

import javax.swing.table.TableColumn;

public class Main {
    /**/*www . j a va  2  s  . c om*/
     * @param table
     * @param cols
     * @see {@link #showColumn(JTable, int)}
     */
    public static void showColumns(JTable table, int[] cols) {
        for (int i = 0; i < cols.length; i++) {
            showColumn(table, cols[i]);
        }
    }

    public static void showColumn(JTable table, int columnIndex) {
        int currentResizeMode = table.getAutoResizeMode();
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        int columnCount = table.getColumnCount();
        if (columnIndex < 0 || columnIndex > columnCount)
            throw new IllegalArgumentException(
                    "TableUtils.hiddenColumn()??????");
        TableColumn tc = table.getColumnModel().getColumn(columnIndex);
        tc.setMaxWidth(300);
        tc.setMinWidth(10);
        tc.setPreferredWidth(100);
        table.setAutoResizeMode(currentResizeMode);
    }
}

Related Tutorials