Search code examples
javaswingjtabletablecellrenderer

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


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


Solution

  • 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));