Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging cells in JTable

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.

like image 853
Ameer Jewdaki Avatar asked Jan 24 '09 21:01

Ameer Jewdaki


1 Answers

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);
 }
(...)
}
like image 197
Pierre Avatar answered Nov 04 '22 04:11

Pierre