Here you can find the source of initializeTableColumns(JTable table, int size[])
public static void initializeTableColumns(JTable table, int size[])
//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]); } } }