> There is two issues that I think are worth considering. If you use one
> table with twice the normal amount of columns and half of the rows, there
> will not be any significant visible separation between the left and right
> halves of the table. Also, the user will be able to move the columns
> around, intermixing the columns from the two halves.
Yes, you are right, I didn't consider those problems. Custom rendering
could possibly address the first issue, I'm not sure of the best way to
fix the second, short of preventing columns from being moved (which may or
may not be acceptable). A third issue would surround how selected rows
are highlighted. You would probably only want the left or right part to
be highlighted. Again, this could be achieved through custom rendering.
> Instead of creating a single table with a single table model filtering
> the
[quoted text clipped - 4 lines]
> would present either the first or second have of the rows of the
> underlying table model.
That is probably a better solution, so long as there aren't any issues
with the way the tables scroll.
Dan.

Signature
Daniel Dyer
http://www.footballpredictions.net
John McGrath - 21 Mar 2005 00:00 GMT
> > Instead of creating a single table with a single table model filtering
> > the values from an underlying table model, I would consider creating
[quoted text clipped - 5 lines]
> That is probably a better solution, so long as there aren't any issues
> with the way the tables scroll.
You will need to position the TableHeaders yourself, side-by-side in a
panel that you install in the JScrollPane's header, just like the JTables
are positioned side-by-side in the JScrollPane's viewport. Once that is
done, I think they will scroll the way you expect them to.

Signature
Regards,
John McGrath
John McGrath - 21 Mar 2005 00:28 GMT
I started thinking about this, and got worried that there may be some
"hidden" gotchas that would crop up. I would hate to convince you that
this approach was easy, and have it turn out to be much more complicated,
so I gave it a try. It turned out to work pretty much as I had expected.
No problems at all. FYI, here is the code:
=========================================================================
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
public class SplitTableTest
extends JPanel
{
public static void main( String[] args ) {
EventQueue.invokeLater( new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setLocation( 200, 200 );
frame.getContentPane().add( new SplitTableTest() );
frame.pack();
frame.setVisible( true );
}
} );
}
public SplitTableTest() {
setLayout( new BorderLayout() );
TableModel baseModel = new TestTableModel();
JTable table0 = new JTable( new SplitTableModel( baseModel, 0, 2 ) );
JTable table1 = new JTable( new SplitTableModel( baseModel, 1, 2 ) );
GridLayout gridLayout = new GridLayout( 1, 2 );
gridLayout.setHgap( 10 );
JPanel headerPanel = new JPanel( gridLayout );
headerPanel.add( table0.getTableHeader() );
headerPanel.add( table1.getTableHeader() );
JPanel tablePanel = new JPanel( gridLayout );
tablePanel.add( table0 );
tablePanel.add( table1 );
JScrollPane scroller = new JScrollPane( tablePanel );
scroller.setColumnHeaderView( headerPanel );
add( scroller );
}
private static final class SplitTableModel
implements TableModel
{
private TableModel baseModel;
private int segment;
private int segments;
public SplitTableModel( TableModel baseModel, int segment,
int segments )
{
this.baseModel = baseModel;
this.segment = segment;
this.segments = segments;
}
public int getRowCount() {
int baseRows = baseModel.getRowCount();
int rows = baseRows / segments;
if ( ( baseRows % segments ) > segment ) {
++ rows;
}
return rows;
}
public int getColumnCount() {
return baseModel.getColumnCount();
}
public String getColumnName( int column ) {
return baseModel.getColumnName( column );
}
public Class getColumnClass( int column ) {
return baseModel.getColumnClass( column );
}
public boolean isCellEditable( int row, int column ) {
return false;
}
public Object getValueAt( int row, int column ) {
return baseModel.getValueAt( mapRow(row), column );
}
public void setValueAt( Object aValue, int row, int column ) {
}
public void addTableModelListener( TableModelListener l ) {
}
public void removeTableModelListener( TableModelListener l ) {
}
private int mapRow( int row ) {
int baseRows = baseModel.getRowCount();
int firstRow = segment * baseRows / segments
+ Math.min( segment, baseRows % segments );
return ( row + firstRow );
}
}
}
=========================================================================

Signature
Regards,
John McGrath
farseer - 21 Mar 2005 01:38 GMT
thanks john,
i really appreciate the code snippet.