How to paint certain lines in a totalcross.ui.Grid

2

When filling a Totalcross Grid, I have conditional records that should be highlighted relative to the others. I thought about changing the color of the line, or showing the font in bold.

Grid by default shows "zebra" lines with white and gray colors interspersed and can be easily modified with the attributes firstStripeColor and secondStripeColor, in addition to the row when selected, with highlightColor. These alternatives do not serve me, because I can have records in sequence to be highlighted. I looked for a bit in the documentation and found the class totalcross.ui.Grid.CellController, this one might answer but I did not find any examples of how to use it.

Can the CellController be used for this, or is there any other alternative to just the Grid?

Implementation:

  • I created a class and extended the CellController, and in the constructor I get a primitive array of integers that indicates which position should be highlighted, and in the getBackColor () method I check if the line position with a value of 1 and assigning the color red:
  •   

    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, I create an array of integers with the size of the list, and inside the loop in the desired condition I assign the value 1 that will be the condition for the line to be detached, instantiate the GridController class created before and hedge in 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(); }

        
    asked by anonymous 23.08.2017 / 20:46

    1 answer

    0

    Yes, Grid.CellController allows you to control the way you want.

    If you downloaded TotalCross from the site, then you may have noticed a src folder where you installed it. Inside it, look for src/main/java/tc/samples/api/ui/GridSample.java .

    In any case, let's take a closer look at Grid.CellController .

    public static abstract class CellController {
    
      public abstract int getForeColor(int row, int col);
      public abstract int getBackColor(int row, int col);
      public abstract String[] getChoices(int row, int col);
      public abstract boolean isEnabled(int row, int col);
    
      public Font getFont(int row, int col) {return null;}
    }
    

    For each method, a row number row and column / col is passed. This means that you can spray your data in the right way. Knowing the row and the column, you can define individually what the background color getBackColor and which color front / color the text getForeColor prints.

    The other methods, for now, seem superfluous for their use.

    Examples

    I'll go over here 4 examples of CellController and their respective screenshots for you to base.

    CellController colunas = new CellController() {
        @Override
        public int getForeColor(int row, int col) {
            return 0;
        }
    
        @Override
        public int getBackColor(int row, int col) {
            return (col % 2 == 0) ? 0xffffff : 0xe0e0e0;
        }
    
        @Override
        public boolean isEnabled(int row, int col) {
            return true;
        }
    
        @Override
        public String[] getChoices(int row, int col) {
            return null;
        }
    });
    

    CellControllertresDois_doisTres=newCellController(){@OverridepublicintgetForeColor(introw,intcol){return0;}@OverridepublicintgetBackColor(introw,intcol){intthreshold;if((row/5)%2==0){threshold=3;}else{threshold=2;}return(row%5)<threshold?0xffffff:0xe0e0e0;}@OverridepublicbooleanisEnabled(introw,intcol){returntrue;}@OverridepublicString[]getChoices(introw,intcol){returnnull;}});

    CellControllerxadrez=newCellController(){@OverridepublicintgetForeColor(introw,intcol){return0;}@OverridepublicintgetBackColor(introw,intcol){return((row+col)%2==0)?0xffffff:0xe0e0e0;}@OverridepublicbooleanisEnabled(introw,intcol){returntrue;}@OverridepublicString[]getChoices(introw,intcol){returnnull;}});

    CellControllerpadrao=newCellController(){@OverridepublicintgetForeColor(introw,intcol){return0;}@OverridepublicintgetBackColor(introw,intcol){return(row%2==0)?0xffffff:0xe0e0e0;}@OverridepublicbooleanisEnabled(introw,intcol){returntrue;}@OverridepublicString[]getChoices(introw,intcol){returnnull;}});

    ThecodeIusedtotakescreenshotsfollows in this snippet .

        
    24.08.2017 / 07:37