Java JTable Column Size Calculate initializeTableColumns(JTable table, int size[])

Here you can find the source of initializeTableColumns(JTable table, int size[])

Description

initialize Table Columns

License

Open Source License

Declaration

public static void initializeTableColumns(JTable table, int size[]) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.swing.*;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

import java.awt.*;

public class Main {
    public static void initializeTableColumns(JTable table, int size[]) {
        TableCellRenderer dcr = table.getDefaultRenderer(Integer.class);
        if (dcr instanceof JLabel) {
            ((JLabel) dcr).setHorizontalAlignment(SwingConstants.CENTER);
        }//from w ww . jav  a2 s  .com
        table.setDefaultRenderer(Integer.class, dcr);
        JTableHeader header = table.getTableHeader();
        if (header != null) {
            TableCellRenderer headerRenderer = header.getDefaultRenderer();
            if (headerRenderer instanceof JLabel) {
                ((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
            }
            header.setPreferredSize(new Dimension(30, 25));
        }
        TableColumn column;
        for (int i = 0; i < size.length; i++) {
            column = table.getColumnModel().getColumn(i);
            column.setPreferredWidth(size[i]);
        }
    }
}

Related

  1. fixedColumSize(TableColumn c, int width)
  2. fixSize(TableColumn column, JTable table)
  3. initColumnSizes(JTable table)
  4. initColumnSizes(JTable table, boolean[] show)
  5. initColumnSizes(JTable table, int rowStart, int rowEnd)
  6. pointOutsidePrefSize(JTable table, int row, int column, Point p)
  7. setSize(JTable table, int columnIndex, int newSize)