3

I have a jxtable. It has horizontalGridLines enabled. here is what it looks like.

Current JXTable

I want the horizontal gridline to be thicker. See the desired look below. The line after the 2nd row should have a thicker divider.

enter image description here

kleopatra
  • 51,061
  • 28
  • 99
  • 211
codeNinja
  • 1,442
  • 3
  • 25
  • 61

2 Answers2

2

You can override the paintComponent method in JXTable. The following example creates a JTable with a line thickness of 3 pixels after the 2nd row:

JXTable table = new JXTable() {
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        // Actual line thickness is: thickness * 2 + 1
        // Increase this as you wish.
        int thickness = 1;

        // Number of rows ABOVE the thick line
        int rowsAbove = 2;

        g.setColor(getGridColor());
        int y = getRowHeight() * rowsAbove - 1;
        g.fillRect(0, y - thickness, getWidth(), thickness * 2 + 1);
    };
};
uyuyuy99
  • 353
  • 4
  • 11
  • 1
    +1 one hack is as good (or bad :-) as another. A couple of suggestions: a) increase the rowHeight of one of the neighboring rows, otherwise the line might be painted over content b) for the painting position, query the table (via getCellRect()) instead of calculating manually – kleopatra Mar 28 '14 at 09:26
  • @kleopatra I originally had code that used `getCellRect()`, but I changed it to make the code simpler. As for increasing the rowHeight, that would work, but it would look weird with 2 lopsided rows. Thanks for the feedback by the way. – uyuyuy99 Mar 28 '14 at 10:31
  • @kleopatra Eh, I guess. But there's not much I see that could go wrong with it. – uyuyuy99 Mar 28 '14 at 20:39
2

The painting of the gridlines is controlled by the table's ui-delegate. There's no way to interfere, all options are hacks.

That said: a SwingX'sh hack would be to use a Highlighter that decorates the renderers with a MatteBorder, if the target row is the second.

table.setShowGrid(true, false);
// apply the decoration for the second row only
HighlightPredicate pr = new HighlightPredicate() {

    @Override
    public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
        return adapter.row == 1;
    }
};
int borderHeight = 5;
// adjust the rowHeight of the second row 
table.setRowHeight(1, table.getRowHeight() + borderHeight);
Border border = new MatteBorder(0, 0, borderHeight, 0, table.getGridColor());
// a BorderHighlighter using the predicate and the MatteBorder
Highlighter hl = new BorderHighlighter(pr, border);
table.addHighlighter(hl);
kleopatra
  • 51,061
  • 28
  • 99
  • 211