Java Utililty Methods JTable Column Size Calculate

List of utility methods to do JTable Column Size Calculate

Description

The list of methods to do JTable Column Size Calculate are organized into topic(s).

Method

intcalculateColumnWidth_1(JTable table, int pos)
calculate Column Widt_
for (int ii = 0; ii < table.getColumnModel().getColumnCount(); ii++) {
    TableColumn col = table.getColumnModel().getColumn(ii);
    if (ii == pos) {
        return col.getHeaderValue().toString().getBytes().length * 10;
throw new RuntimeException("error!!");
voidfixedColumSize(TableColumn c, int width)
fixed Colum Size
c.setMinWidth(width);
c.setMaxWidth(width);
voidfixSize(TableColumn column, JTable table)
Fix the size of a column according to the header text.
TableCellRenderer rndr;
rndr = column.getHeaderRenderer();
if (rndr == null) {
    rndr = table.getTableHeader().getDefaultRenderer();
Component c = rndr.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1,
        column.getModelIndex());
int width = c.getPreferredSize().width + 10;
...
voidinitColumnSizes(JTable table)
init Column Sizes
TableModel model = (TableModel) table.getModel();
Object[] longValues = new Object[model.getColumnCount()];
for (int i = 0; i < model.getColumnCount(); i++) {
    longValues[i] = table.getModel().getColumnName(i);
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
...
intinitColumnSizes(JTable table, boolean[] show)
init Column Sizes
TableColumn column = null;
Component comp = null;
int cellWidth = 0;
TableModel model = table.getModel();
int numCols = model.getColumnCount();
int numRows = model.getRowCount();
if (show != null && show.length != numCols)
    show = null;
...
voidinitColumnSizes(JTable table, int rowStart, int rowEnd)
Sets the width of the table columns to fit in the table data.
boolean containsData = table.getRowCount() > 0;
Component cellRendererComponent;
int headerWidth;
int cellWidth;
TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
for (int columnIndex = 0; columnIndex < table.getColumnCount(); columnIndex++) {
    TableColumn column = table.getColumnModel().getColumn(columnIndex);
    cellRendererComponent = headerRenderer.getTableCellRendererComponent(table, column.getHeaderValue(),
...
voidinitializeTableColumns(JTable table, int size[])
initialize Table Columns
TableCellRenderer dcr = table.getDefaultRenderer(Integer.class);
if (dcr instanceof JLabel) {
    ((JLabel) dcr).setHorizontalAlignment(SwingConstants.CENTER);
table.setDefaultRenderer(Integer.class, dcr);
JTableHeader header = table.getTableHeader();
if (header != null) {
    TableCellRenderer headerRenderer = header.getDefaultRenderer();
...
booleanpointOutsidePrefSize(JTable table, int row, int column, Point p)
point Outside Pref Size
if ((table.convertColumnIndexToModel(column) != 0) || (row == -1)) {
    return true;
TableCellRenderer tcr = table.getCellRenderer(row, column);
Object value = table.getValueAt(row, column);
Component cell = tcr.getTableCellRendererComponent(table, value, false, false, row, column);
Dimension itemSize = cell.getPreferredSize();
Rectangle cellBounds = table.getCellRect(row, column, false);
...
voidsetSize(JTable table, int columnIndex, int newSize)
set Size
TableColumn column = table.getColumnModel().getColumn(columnIndex);
setSize(table, column, newSize);