Add cellpadding to a Java JTable
Asked Answered
F

1

2

I'm trying to implement a Swing JTable. I followed the tuorial on http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#simple

I want the table cell's not to be editable (this works) and I want the table cells to have more padding to it's borders. Like cellpadding in HTML.

This is part of my code and the cellpadding thing doesn't work.

class BoardTableCellRenderer extends DefaultTableCellRenderer {

    private static final long serialVersionUID = 1L;

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
        setBorder(BorderFactory.createEmptyBorder(15,15,15,15));
        return this;
    }
}

String[] columnNames = {"Datei",
        "Zeile",
        "Zeichen",
        "Fehler", "test"};

Object[][] data = {
        {"Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false)},
        {"John", "Doe", "Rowing", new Integer(3), new Boolean(true)},
        {"Sue", "Black", "Knitting", new Integer(2), new Boolean(false)},
        {"Jane", "White", "Speed reading", new Integer(20), new Boolean(true)},
        {"Joe", "Brown", "Pool", new Integer(10), new Boolean(false)}
    };

JTable table = new JTable(data, columnNames){
    private static final long serialVersionUID = -4430174981226468686L;

    @Override
    public boolean isCellEditable(int arg0, int arg1) {
        return false;
    }};

table.setAutoCreateRowSorter(true);
table.getTableHeader().setReorderingAllowed(false);
table.setDefaultRenderer(String.class, new BoardTableCellRenderer());

table is placed on a JScrollPane. The table is displayed, the cells are not editable but the cellpadding is not applied!

Can anyone help? Thanks :)

Fluorspar answered 13/11, 2013 at 23:6 Comment(4)
For better help sooner, post an SSCCE.Pigmy
You can try three things. You can try supplying a custom cell renderer that has an EmptyBorder applied to it, for every column, a cell editor doing the same thing and/or change the row height property of the tableTopmost
For example.Buckden
As per OP's comment in tenorsax's answer, it appears that in fact you can't use this border technique to change the top/bottom padding. If so you have two options: row height and/or setIntercellSpacing.Thrawn
C
7

Default implementation of getColumnClass() in DefaultTableModel which is used by JTable (by default) returns Object.class. That is the reason BoardTableCellRenderer is not used, as you're setting it up for columns with String.class.

You may override getColumnClass. Or in case of this sample, replace:

table.setDefaultRenderer(String.class, new BoardTableCellRenderer());

with:

table.setDefaultRenderer(Object.class, new BoardTableCellRenderer());

to see the effect of BoardTableCellRenderer.

Chattel answered 13/11, 2013 at 23:57 Comment(1)
Thanks, that kind of worked... However, the 15px Border within the custom BoardTableCellRenderer() did only render left/right padding and not top/bottom. Fot top/bottom I had to add setRowHeight(25) on the JTable itself...Fluorspar

© 2022 - 2024 — McMap. All rights reserved.