I would like to add a right-padding to a JTable column, is it possible?

Chad picture Chad · Jun 19, 2013 · Viewed 13.2k times · Source

I want to add right cell-padding to a column in my JTable, how do I do it?

I tried searching online but I can't seem to find a definitive answer for this.

I hope someone can help me.

Regards, Chad

Answer

trashgod picture trashgod · Jun 19, 2013

Use a custom TableCellRenderer, and specify setHorizontalAlignment(JLabel.RIGHT). There's a related example here that illustrates JLabel.CENTER.

Addendum: My problem is padding and not alignment.

If you want the padding inside the cell, rather than between cells, you can use a border in the renderer, as @Guillaume Polet suggests. Note that the border can be asymmetric; the example below pads only on the right.

setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));