Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to left or right justify a column header in a JTable

Tags:

java

swing

I searched and couldn't find an answer for this. By default column headers in a JTable are centered. How do I make certain column headers left or right justified instead?

TIA

like image 691
sproketboy Avatar asked Jul 01 '10 11:07

sproketboy


People also ask

How do you make a JTable column not editable?

Right-click on the table cells. From popup menu, choose "Table Contents..". Uncheck the editable check box for the column you want to make it non-editable.

What is JTable in swing?

The JTable class is a part of Java Swing Package and is generally used to display or edit two-dimensional data that is having both rows and columns. It is similar to a spreadsheet. This arranges data in a tabular form.


1 Answers

TableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setHorizontalAlignment(SwingConstants.RIGHT);
table.getColumn(id).setHeaderRenderer(renderer);

Or, if you don't have the column names available:

table.getColumnModel().getColumn(index).setHeaderRenderer(renderer);
like image 70
Michael Borgwardt Avatar answered Oct 23 '22 23:10

Michael Borgwardt