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

Asked

Viewed 75 times

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?

inserir a descrição da imagem aqui


Implementation:

  1. 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(); }

1 answer

0


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

If you downloaded Totalcross from the site, then you must have noticed a folder src in the place where you install it. Inside, look for src/main/java/tc/samples/api/ui/GridSample.java.

In any case, let’s examine the 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 line number is passed/Row and column/col. This means you can spray your data properly. Knowing the row and column, you can individually define which the background color getBackColor and what is the front color/the color of the text getForeColor.

The other methods, for the time being, seem superfluous for their use.

Examples

I go this way 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;
    }
});

grid de colunas marcadas

CellController tresDois_doisTres = new CellController() {
    @Override
    public int getForeColor(int row, int col) {
        return 0;
    }

    @Override
    public int getBackColor(int row, int col) {
        int threshold;
        if ((row / 5) % 2 == 0) {
            threshold = 3;
        } else {
            threshold = 2;
        }

        return (row % 5) < threshold ? 0xffffff : 0xe0e0e0;
    }

    @Override
    public boolean isEnabled(int row, int col) {
        return true;
    }

    @Override
    public String[] getChoices(int row, int col) {
        return null;
    }
});

alternando: 3 linhas brancas e 2 linhas cinzas, depois 2 linhas brancas e 3 linhas cinzas

CellController xadrez = new CellController() {
    @Override
    public int getForeColor(int row, int col) {
        return 0;
    }

    @Override
    public int getBackColor(int row, int col) {
        return ((row + 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;
    }
});

marcando células em xadrez

CellController padrao = new CellController() {
    @Override
    public int getForeColor(int row, int col) {
        return 0;
    }

    @Override
    public int getBackColor(int row, int col) {
        return (row % 2 == 0) ? 0xffffff : 0xe0e0e0;
    }

    @Override
    public boolean isEnabled(int row, int col) {
        return true;
    }

    @Override
    public String[] getChoices(int row, int col) {
        return null;
    }
});

padrão: linhas de cores alternadas

The code I used to take the screenshots follows in this snippet.

  • 1

    Solved @Jefferson Quesado, thanks for the help. I will post my implementation (editing my question) to help those who need it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.