Daniel Pereira Daniel Pereira - 6 months ago 29
Java Question

How to center in JTable cell a value?

How to center a value in

JTable
cell? I'm using Netbeans.

Answer

You need to customize the renderer. To center the first column you can do:

DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment( JLabel.CENTER );
table.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );

To center all columns with String data you can do:

DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment( JLabel.CENTER );
table.setDefaultRenderer(String.class, centerRenderer);
Comments