Skip to content
Qualtagh edited this page Jul 11, 2020 · 3 revisions

Let's take our simple quick start example and fix (dock, freeze) the left side columns group, so it remains untouched while a user scrolls the right side of the table.

This fixation ability is an adaptation of this example for JTables given by Rob Camick.

All you need to do is to set the fixed property of a column (or a columns group):

new ModelFieldGroup( "A", "<html>A" )
  .withChild( new ModelField( "B", "<html>B" ).withFixed( true ) ) // The whole group "A" would become fixed.
  .withChild( new ModelField( "C", "<html>C" ) )

Making a child fixed also makes its parent fixed. Fixing a parent affects all its children. So, you can choose any column of a group to fix the whole group.

Earlier, we used this construction to add a table to a container:

frame.add( new JScrollPane( table ) );

Fixed columns are a separate table that is placed at the left viewport of a scroll pane. So, fixing of columns affects a scroll pane. A new method was introduced to simplify a scroll pane manipulations. Now you should call:

frame.add( table.getScrollPane() );

That's it! Nothing else is needed.

Result

Note that getScrollPane() simply creates and stores a new JScrollPane if fixing of columns is not required. In other words, you can always call getScrollPane() instead of creating a new one manually.

If you need to change some properties of the left side fixed part of table, you may call table.getSlaveTable() for this. Use fixed.getMasterTable() on a fixed table part to retrieve the main table. Note that these methods return null if there's no corresponding table.

What you may want to do with the fixed part?

Setting autoResizeMode:

JBroTable fixed = table.getSlaveTable();
if ( fixed != null ) {
  fixed.setAutoResizeMode( JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS );
  // or fixed.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
}

Disabling (hiding) row selection without affecting a right side:

fixed.setSelectionBackground( fixed.getBackground() );
fixed.setSelectionForeground( fixed.getForeground() );

Row height, row margin, vertical lines presence, selection model, model, data, row sorter and fillsViewportHeight property are updated automatically in fixed side when the main table changes.

If you want to set a different row height, or if you want some other of these properties of left side to differ from the right side, you should invoke changing of the left side after changing the right side.

The rest properties are not synchronized.

A full source code of this example.