2
When filling a Totalcross Grid, I have conditioned records that should be highlighted in relation to the others. I thought to change the line color, or show the font in bold.
Grid by default shows "zebra" lines with white and gray interspersed colors that can be easily modified with firstStripeColor and secondStripeColor attributes, plus line when selected, with highlightColor. These alternatives do not serve me, because I can have records in sequence to be highlighted. I looked for some in the documentation and found the class totalcross.ui.Grid.Cellcontroller, this might fit but I couldn’t find any example of how to use it.
Is it possible to use the Cellcontroller for this, or is there an alternative only with the Grid?
Implementation:
- I created a class and extended the Cellcontroller, and in the constructor I get a primitive integer array that indicates which position should be highlighted, and in the getBackColor() method I check whether the position of the line with value 1 and assigning the red color:
public class GridController extends CellController { int[] highlight; public GridController(int[] highlight) { this.highlight = highlight; } @Override public int getBackColor(int row, int col) { return (highlight[row] == 1) ? Color.RED : -1; } @Override public String[] getChoices(int arg0, int arg1) { return null; } @Override public int getForeColor(int arg0, int arg1) { return 0; } @Override public boolean isEnabled(int arg0, int arg1) { return true; } }
And to use, create an array of integers with the size of the list, and within the loop in the desired condition assign the value 1 that will be the condition for the line to be highlighted, instantiating the Gridcontroller class created before and Seto on the grid :
private void buildGrid() { int highlight[] = new int[lista.size()]; for (int i = 0; i < lista.size(); i++) { X x = lista[i]; if (x.getOverdue().isAfter(x.getDueDate())) highlight[i] = 1; grid.add(new String[] { x.getId(), x.getDescricao() }); } GridController gridController = new GridController(highlight); grid.setCellController(gridController); repaint(); }
Solved @Jefferson Quesado, thanks for the help. I will post my implementation (editing my question) to help those who need it.
– Karpinski