Skip to content Skip to sidebar Skip to footer

Merging Cells In Jtable

Is it possible to merge some cells of a JTable object? (source: codeguru.com) If it's not possible through JTable what is the best approach. Thanks.

Solution 1:

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

classModel2extendsAbstractTableModel
{
private TableModel delegate;
publicModel2(TableModel delegate)
 {
 this.delegate= delegate;
 }

publicintgetRowCount() { returnthis.delegate.getRowCount();}
publicintgetColumnCount() { returnthis.delegate.getColumnCount()-1;}
public Object getValueAt(int row, int col)
 {
 if(col==0) return""+delegate.getValueAt(row,col)+delegate.getValueAt(row,col+1);
 returndelegate.getValueAt(col+1);
 }
(...)
}

Solution 2:

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.

Post a Comment for "Merging Cells In Jtable"