1

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?

Bitmap
  • 12,402
  • 16
  • 64
  • 91
  • it's not clear what exactly you are after (_alignment of header and the actual row cell values looks slighly off_ - not only slightly, the header is centered :-) - best to show an SSCCE to demonstrate the issue (as @mKorbel already suggested) – kleopatra Apr 18 '12 at 12:35
  • I am doing exactly the same thing as @mKorbel, the only difference in my code is, I have sized up two of my columns. – Bitmap Apr 18 '12 at 12:47
  • but in his example there is no text (that's what you are after?) alignment (between header/cell) at all – kleopatra Apr 18 '12 at 12:52

1 Answers1

1

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();
            }
        });
    }
}
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I did try that, but the requirement here do not accept the approach if you see what I mean. – Bitmap Apr 18 '12 at 09:40
  • 1
    @Bitmap I havent't any issue with that, but maybe there is/are another issue(s), you can use linked code for creating a [SSCCE](http://sscce.org/), maybe someone .... – mKorbel Apr 18 '12 at 10:50
  • 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. – Bitmap Apr 18 '12 at 11:04
  • I have set gapWidth to 5 and seems to align with cell data. – Bitmap Apr 18 '12 at 12:49