Java: Auto Adjust JTable Columns

Posted on

Perhaps the most common task to be done by a programmer after creating a JTable is to figure out how to adjust the columns to make it look good. I can not remember how many times I have created applications with many JTables and then came to the point where I needed to adjust the columns. At times you may also want to hide certain columns. This stuff comes out of the box with applications like Excel.

After extensive research and testing code (this was years ago by the way) I have come up with some nice code to adjust the columns of a JTable in a reusable way. Just call the method adjustTableColumns() and give it some parameters. See the code below to see how it works and what parameters to send.

public void adjustTableColumns(JTable table) {
	for(int i=0; i < table.getColumnCount(); i++) {
		adjustColumn(i, 5, table);
	}
}
	
public void adjustTableColumns(JTable table, int ... hiddenColumns) {
	for(int i=0; i < table.getColumnCount(); i++) {
                adjustColumn(i, 5, table, hiddenColumns);
        }
}
	
private void adjustColumn(int columnIndex, int margin, JTable table) {
	int width = 0;
	DefaultTableColumnModel colModel = (DefaultTableColumnModel)table.getColumnModel();
	TableColumn column = colModel.getColumn(columnIndex);

	TableCellRenderer renderer = column.getHeaderRenderer();
	if(renderer == null) {
		renderer = table.getTableHeader().getDefaultRenderer();
	}

	Component component = renderer.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, 0);
	width = component.getPreferredSize().width;

	for(int i=1; i < table.getRowCount(); i++) {
		renderer = table.getCellRenderer(i, columnIndex);
		component = renderer.getTableCellRendererComponent(table, table.getValueAt(i, columnIndex), false, false, i, columnIndex);
		width = Math.max(width, component.getPreferredSize().width);
	}

	width += 2*margin;
	column.setPreferredWidth(width);
}

private void adjustColumn(int columnIndex, int margin, JTable table, int ... hiddenColumns) {
	int width = 0;
	DefaultTableColumnModel colModel = (DefaultTableColumnModel)table.getColumnModel();
	TableColumn column = colModel.getColumn(columnIndex);

	for(int j = 0; j < hiddenColumns.length; j++) {
		if(hiddenColumns[j] == columnIndex) {
			column.setMaxWidth(0);
			column.setMinWidth(0);
			column.setPreferredWidth(0);
			column.setResizable(false);
		}
		else {
			TableCellRenderer renderer = column.getHeaderRenderer();
			if(renderer == null) {
				renderer = table.getTableHeader().getDefaultRenderer();
			}

			Component component = renderer.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, 0);
			width = component.getPreferredSize().width;

			for(int i=1; i < table.getRowCount(); i++) {
				renderer = table.getCellRenderer(i, columnIndex);
				component = renderer.getTableCellRendererComponent(table, table.getValueAt(i, columnIndex), false, false, i, columnIndex);
				width = Math.max(width, component.getPreferredSize().width);
			}

			width += 2*margin;
			column.setPreferredWidth(width);
		}
	}
}
Advertisements

One thought on “Java: Auto Adjust JTable Columns

    emi said:
    June 24, 2014 at 9:44 pm

    GREAT

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s