9

Is it possible to merge some cells of a JTable object?

merging cells
(source: codeguru.com)

If it's not possible through JTable what is the best approach. Thanks.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Ameer Jewdaki
  • 1,758
  • 4
  • 21
  • 36

2 Answers2

2

You could implement a JTable using a TableModel merging two columns of the original TableModel.

class Model2 extends AbstractTableModel
{
private TableModel delegate;
public Model2(TableModel delegate)
 {
 this.delegate= delegate;
 }

public int getRowCount() { return this.delegate.getRowCount();}
public int getColumnCount() { return this.delegate.getColumnCount()-1;}
public Object getValueAt(int row, int col)
 {
 if(col==0) return ""+delegate.getValueAt(row,col)+delegate.getValueAt(row,col+1);
 return delegate.getValueAt(col+1);
 }
(...)
}
Pierre
  • 34,472
  • 31
  • 113
  • 192
-1

Not out-of-the-box. Here is an example that supports merging arbitrarty cells. This page has several examples of tables with spanning cells. Of course it's old and you get what you pay for. If paid software is an option, JIDE Grids has some really nice Swing table support including custom cell spans.

Dave Ray
  • 39,616
  • 7
  • 83
  • 82