Search code examples
javaswingjtablejtableheader

Spacing JTable header


I know spacing JTable cells are pretty straight forward as shown below;

int gapWidth = 10;
int gapHeight = 5;
table.setIntercellSpacing(new Dimension(gapWidth, gapHeight));

Spacing like this doesn't seems to affect the tables header. Is there a way to space the header with the same dimension?


Solution

  • Easiest way could be to set EmptyBorders to the JTableHeader inside the TableCellRenderer

    The code above does my spacing, but the alignment of header and the actual row cell values looks slighly off - as a result of spacing only placed on cells but not the header.

    I added a SSCCE (3mins 27seconds inc. uploading here)

    enter image description hereenter image description here

    from code

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class IntercellSpacingTableHeader {
    
        private JFrame frame = new JFrame("Table Demo");
        private String[] columnNames = {"String", "Integer", "Float", "Double"};
        private Object[][] data = {
            {"aaa", new Integer(12), new Float(12.15), new Double(100.05)},
            {"bbb", new Integer(5), new Float(7.154), new Double(6.1555)},
            {"CCC", new Integer(92), new Float(0.1135), new Double(3.1455)},
            {"ddd", new Integer(12), new Float(31.15), new Double(10.05)},
            {"eee", new Integer(5), new Float(5.154), new Double(16.1555)},
            {"fff", new Integer(92), new Float(4.1135), new Double(31.1455)}};
        private TableModel model = new DefaultTableModel(data, columnNames) {
    
            private static final long serialVersionUID = 1L;
    
            @Override
            public Class<?> getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };
        private JTable table = new JTable(model);
    
        public IntercellSpacingTableHeader() {
            int gapWidth = 10;
            int gapHeight = 5;
            table.setIntercellSpacing(new Dimension(gapWidth, gapHeight));
            table.setRowHeight(20);
            table.setPreferredScrollableViewportSize(table.getPreferredSize());
            JScrollPane scroll = new JScrollPane(table);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(scroll);
            frame.pack();
            frame.setLocation(150, 150);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new IntercellSpacingTableHeader();
                }
            });
        }
    }