NatTable - need checkbox only when editable

679 views Asked by At

I am new to NatTable. I have gone thorough the NatTable examples and its source code but am not getting a solution to one of my problems. In the NatTable I have a column that should provide a checkbox for selection depending on the value of another column. I have used Checkboxpainter, checkboxcelleditor, the defaultbooleanconverter and IEditableRule. This renders a checkbox irrespective of whether the cell is editable or not though it allows me to mark the checkbox only if it is enabled.

However as per our requirement user should not see the checkbox if the row is not selectable. or in worst case a disabledcheckbox should be rendered for rows that are not selectable.

Can someone please help me out?

Thanks and regards,

Pradyumna

2

There are 2 answers

0
praddyB On BEST ANSWER

Got a solution for this. I had to write a custom checkboxpainter (inheriting from the one that is available OOTB) and override its getImage method to return null for appropriate cells

0
Stefan Winkler On

There is a better solution which I have just applied to a similar case in my work.

I did it by adding the following configuration to the table:

// make checkbox cells editable
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITABLE_RULE, IEditableRule.ALWAYS_EDITABLE, DisplayMode.EDIT, CONFIG_LABEL_CHECKBOX);

// register the checkbox editor for DisplayMode.EDIT
configRegistry.registerConfigAttribute(EditConfigAttributes.CELL_EDITOR, new CheckBoxCellEditor(), DisplayMode.EDIT, CONFIG_LABEL_CHECKBOX);

// register the checkbox painter for DisplayMode.NORMAL
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new CheckBoxPainter(), DisplayMode.NORMAL, CONFIG_LABEL_CHECKBOX);

// register the painter for empty cells in DisplayMode.NORMAL
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new BackgroundPainter(), DisplayMode.NORMAL, CONFIG_LABEL_EMPTY);

Basically, this introduces the configuration labels CONFIG_LABEL_CHECKBOX for an editable checkbox, and CONFIG_LABEL_EMPTY for an empty cell.

Now all you have to do is to attach an IConfigLabelAccumulator to your bodyDataLayer:

bodyDataLayer.setConfigLabelAccumulator(new IConfigLabelAccumulator()
{
  public void accumulateConfigLabels(LabelStack configLabels, int columnPosition, int rowPosition)
  {
    if(columnPosition == CHECKBOX_COLUMN_INDEX) 
    {
      if(someCodeToCheckIfRowIsEditable(rowPosition)) 
      {
        configLabels.add(CONFIG_LABEL_CHECKBOX); 
      }
      else
      {
        configLabels.add(CONFIG_LABEL_EMPTY);            
      }
    }
  }
}