JTable with a groupable header.
Standard Java Swing implementation of a table (JTable) allows to have only one header row by default. It's quite hard to customize a JTable to have more than one row in a header. The JBroTable library lets you do it with ease!
The implementation is based on this StackOverflow answer by MadProgrammer.
Supported Look & Feels:
- Nimbus
- Windows
- Windows Classic
- Motif
- Metal (high contrast, steel, ocean)
- GTK
- Aqua
Tested on Windows 2003, Vista, 7, 8, 10 with standard (Luna, Aero, Metro) and with classic themes. Tested on OpenSUSE 11 with GNOME (GTK+ 2). Tested on Mac OS X 10.8.
Supported features (original StackOverflow answer):
- Windows Classic and MacOS Aqua Look & Feels
- Support for 2 rows in a header
- Stops dragging a column at table end
- Stops dragging a column at columns group end
New features:
- Arbitrary number of rows in a header
- Stops dragging at freezed column (such columns may be set in a table model via API)
- Generic support for L&Fs (it means they're not reimplemented, they're used as delegates for rendering)
- Easy to use table model API
- Table header cells highlighting on mouse over
- Table header cells etching on mouse dragging
- Table cells spans API to merge cells inside a table itself (not in a header)
- Drawing of dragged columns group in a table itself (not only in a header)
- Custom header cell renderer support
- Partial test coverage
- Setting custom rowspan. Rowspan is calculated automatically in a greedy way, but sometimes such a picture is needed (and it can't be calculated automatically):
ASCII (if a picture is not shown):
+---------------+---------------+
| A | |
+-------+-------+ D |
| | | |
| B | C +-------+-------+
| | | E | F |
+-------+-------+-------+-------+
Extra features based on other sources:
- Columns fixed at the left side of the table (based on this answer given by Rob Camick)
Dependencies:
- Java 6 and higher
- Apache Log4J
- JUnit (for tests only)
Getting started:
- Ant: download latest release (ready to use jar file)
- Maven: add the following dependency to your
pom.xml:
<dependency>
<groupId>io.github.qualtagh.swing.table</groupId>
<artifactId>JBroTable</artifactId>
<version>2.0.1</version>
</dependency>Sample usage:
packageio.github.qualtagh.swing.table.view;
importjava.awt.FlowLayout;
importjavax.swing.JFrame;
importjavax.swing.UIManager;
importio.github.qualtagh.swing.table.model.IModelFieldGroup;
importio.github.qualtagh.swing.table.model.ModelData;
importio.github.qualtagh.swing.table.model.ModelField;
importio.github.qualtagh.swing.table.model.ModelFieldGroup;
importio.github.qualtagh.swing.table.model.ModelRow;
importio.github.qualtagh.swing.table.view.JBroTable;
publicclassSample {
publicstaticvoidmain( Stringargs[] ) throwsException {
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
// Hierarchically create columns and column groups.// Each node of columns tree is an instance of IModelFieldGroup.// Leafs are always ModelFields.// Roots can be either ModelFields or ModelFieldGroups.IModelFieldGroupgroups[] = newIModelFieldGroup[] {
newModelFieldGroup( "A", "A" )
.withChild( newModelField( "B", "B" ) )
.withChild( newModelField( "C", "C" ).withRowspan( 2 ) ), // Custom rowspan set.newModelFieldGroup( "D", "D" )
.withChild( newModelField( "E", "E" ) )
.withChild( newModelField( "F", "F" ) ),
newModelField( "G", "G" ),
newModelFieldGroup( "H", "H" )
.withChild( newModelFieldGroup( "I", "I" )
.withChild( newModelField( "J", "J" ) ) )
.withChild( newModelField( "K", "K" ) )
.withChild( newModelFieldGroup( "L", "L" )
.withChild( newModelField( "M", "M" ) )
.withChild( newModelField( "N", "N" ) ) )
};
// Get leafs of columns tree.ModelFieldfields[] = ModelFieldGroup.getBottomFields( groups );
// Sample data.ModelRowrows[] = newModelRow[ 10 ];
for ( inti = 0; i < rows.length; i++ ) {
rows[ i ] = newModelRow( fields.length );
for ( intj = 0; j < fields.length; j++ )
rows[ i ].setValue( j, fields[ j ].getCaption() + i );
}
// Table.ModelDatadata = newModelData( groups );
data.setRows( rows );
JBroTabletable = newJBroTable( data );
// Window.JFrameframe = newJFrame( "Test" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setLayout( newFlowLayout() );
frame.add( table.getScrollPane() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}Result (Windows 8 theme):
More screenshots with different L&Fs are available at wiki.
Animated demo (2.5M)
Take a tour through tutorials page for more complicated examples.
Released into public domain.

