Package swingtree

Class RenderBuilder<C extends JComponent,E>

java.lang.Object
swingtree.RenderBuilder<C,E>
Type Parameters:
C - The type of the component which is used to render the cell.
E - The type of the value of the cell.

public final class RenderBuilder<C extends JComponent,E> extends Object
A builder type for creating cell renderer for a list, combo box or table using a fluent API, typically through methods like UIForList.withRenderer(Configurator), UIForCombo.withRenderer(Configurator) or UIForTable.withRenderer(Configurator), where the builder is exposed to the configurator lambda.

A typical usage of this API may look something like this:


      .withRenderer( it -> it
          .when( Number.class )
          .asText( cell -> cell.valueAsString().orElse("")+" km/h" )
          .when( String.class )
          .as( cell -> {
              // do component based rendering:
              cell.setRenderer( new JLabel( cell.valueAsString().orElse("") ) );
              // or do 2D graphics rendering directly:
              cell.setRenderer( g -> {
              	// draw something
                  g.setColor( UI.color( cell.valueAsString().orElse("") ) );
                  g.fillRect( 0, 0, cell.getComponent().getWidth(), cell.getComponent().getHeight() );
              });
          })
      )
  

Please take a look at the living swing-tree documentation where you can browse a collection of examples demonstrating how to use the API of this class.

  • Method Details

    • when

      public <T extends E> RenderAs<C,E,T> when(Class<T> valueType)
      Use this to specify for which type of cell value you want custom rendering next. The object returned by this method allows you to specify how to render the values.
      Type Parameters:
      T - The type parameter of the cell value, for which you want custom rendering.
      Parameters:
      valueType - The type of cell value, for which you want custom rendering.
      Returns:
      The RenderAs builder API step which expects you to provide a lambda for customizing how a cell is rendered.
    • when

      public <T extends E> RenderAs<C,E,T> when(Class<T> valueType, Predicate<CellDelegate<C,T>> valueValidator)
      Use this to specify a specific type for which you want custom rendering as well as a predicate which tests if a cell value should be rendered. The object returned by this method allows you to specify how to render the values using methods like RenderAs.as(Configurator) or RenderAs.asText(Function).
      Type Parameters:
      T - The type parameter of the cell value, for which you want custom rendering.
      Parameters:
      valueType - The type of cell value, for which you want custom rendering.
      valueValidator - A predicate which should return true if the cell value should be rendered.
      Returns:
      The RenderAs builder API step which expects you to provide a lambda for customizing how a cell is rendered.