set JTable Column Width - Java Swing

Java examples for Swing:JTable Column

Description

set JTable Column Width

Demo Code


//package com.java2s;

import javax.swing.JTable;

import javax.swing.table.TableColumn;

public class Main {
    public static void setColumnWidth(JTable table,
            int graphicalColumnIndex, int width) {
        TableColumn tableColumn = getTableColumn(table,
                graphicalColumnIndex);//from  w ww  .  ja  v a 2  s.  c  om
        if (tableColumn.getPreferredWidth() != width) {
            tableColumn.setPreferredWidth(width);
        }
    }

    public static TableColumn getTableColumn(JTable table,
            int graphicalColumnIndex) {
        return table.getColumnModel().getColumn(graphicalColumnIndex);
    }
}

Related Tutorials