Skip to content
Qualtagh edited this page Aug 1, 2020 · 8 revisions

If you use Ant, download the latest release and add the JBroTable-vX.Y.Z.jar to your classpath.

If you use Maven, add the following dependency to your pom.xml file:

<dependency>
  <groupId>io.github.qualtagh.swing.table</groupId>
  <artifactId>JBroTable</artifactId>
  <version>2.0.1</version>
</dependency>

First of all, create a column model:

IModelFieldGroup groups[] = new IModelFieldGroup[] {
  new ModelField( "USER_ID", "User identifier" ),
  new ModelField( "NAME", "Person name" ),
  new ModelField( "PHONE", "Phone number" )
};

It's simply an array of columns. Each column has an identifier and a caption. Identifier must be unique per array. It's usually gained from a database or is predefined. A caption is a text that would be written on table header cell. Identifier is invisible to user. A caption is shown on screen.

Let's add some grouping to columns:

IModelFieldGroup groups[] = new IModelFieldGroup[] {
  new ModelField( "USER_ID", "User identifier" ),
  new ModelFieldGroup( "NAME", "Person name" )
    .withChild( new ModelField( "FIRST_NAME", "First name" ) )
    .withChild( new ModelField( "LAST_NAME", "Last name" ) ),
  new ModelField( "PHONE", "Phone number" )
};

This would turn our column NAME into a column group NAME with two child columns FIRST_NAME and LAST_NAME.

Then we need to add some data to our table. Write this line to obtain all columns:

ModelField fields[] = ModelFieldGroup.getBottomFields( groups );

This code would return only leaf nodes: USER_ID, FIRST_NAME, LAST_NAME and PHONE. It means that our table would have 4 columns.

Generation of 10 rows without data:

ModelRow rows[] = new ModelRow[ 10 ];
for ( int i = 0; i < rows.length; i++ )
  rows[ i ] = new ModelRow( fields.length );

Creation of table model:

ModelData data = new ModelData( groups );
data.setRows( rows );

Filling in some data:

data.setValue( 0, "FIRST_NAME", "John" );
data.setValue( 0, "LAST_NAME", "Doe" );
data.setValue( 1, "FIRST_NAME", "Jane" );
data.setValue( 1, "LAST_NAME", "Doe" );

Creation of a table itself and adding it to a container:

JBroTable table = new JBroTable( data );
frame.add( new JScrollPane( table ) );

The result looks like this:

Result

The whole source code of this tutorial can be found here.