Tablecellrenderer, problem in color display in table

Asked

Viewed 51 times

1

Here’s the thing, I want to change the font color a specific part of my table when the user’s payment is up to date to blue. Otherwise, I want it to change to red. I want only the "situation" column to do that. That is, when I find "ok" I want to change to blue or to red when I find "Pendant".

// Esse é meu Renderer
public class pagamentoTableCellRenderer extends JLabel implements TableCellRenderer {
    // lista de objeto dos usuários
    private ArrayList<ModelUsuario> listaAluno;

    public pagamentoTableCellRenderer(ArrayList<ModelUsuario> situacao) {
        this.listaAluno = situacao;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        ModelUsuario situacao = listaAluno.get(row);

        if (situacao.getSituacao().equals("ok")) {

            setForeground(Color.blue);

        } else {
            setForeground(Color.red);
        }

        return this;
    }
}



public class ModeltabelaCarregar {


     public static  void main(String [] args) throws ParseException{
         new viewControleDePagamento().setVisible(true);;

    }

}

2 answers

0


In this case you instantiate a Defaulttablecellrenderer object thus:

DefaultTableCellRenderer colorRenderer = new DefaultTableCellRenderer() {

        @Override
        public void setValue(Object value) {
            switch ((String) value) {
                case "Sim":
                    setBackground(Color.YELLOW);


                    break;
                case "Não":
                    setBackground(Color.WHITE);


                    break;
            }
            super.setValue(value);
            setForeground(Color.BLACK);
        }
    };

The parameter of the setCellRenderer method requires a Tablecellrenderer argument:

suaTabela.getColumnModel().getColumn(7).setCellRenderer(colorRenderer);

Result is this:

Resultado

0

Do it like this:

public class ColorirTabelaTramitacao extends DefaultTableCellRenderer {

/**
 * 
 */
private static final long   serialVersionUID    = 1L;
public static final float R = 0.9f;
public static final float G = 0.5f; 
public static final float B = 0.8f;

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
 super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
   //203,216,227
    //Color c = new Color(203,216,227);

    Color c = Color.WHITE;
    Object text = table.getValueAt(row, 3);
    Object text2 = table.getValueAt(row, 10);
    if (text != null && "LICENÇA EMITIDA".equals(text.toString())){
        // RGB
        c = new Color(126,195 ,255);
    }
    if(text != null && "DESARQUIVADO".equals(text.toString())){
        c = Color.GREEN;
    }
    if(text != null && "ARQUIVADO".equals(text.toString())){
        c = Color.RED;
    }
    if(text2 != null){
        c = Color.YELLOW;
    }

       setBackground(c);      


   if (isSelected){
          //setForeground(Color.BLUE);
          setBackground(table.getSelectionBackground());    
  }



    return this;


}

}

Call it that:

private void defineModelo() {
        tableModel = (DefaultTableModel) tblTramitacao.getModel();

        listModel = tblTramitacao.getSelectionModel();    

        tblTramitacao.setDefaultRenderer(Object.class, new ColorirTabelaTramitacao());

It’ll stay that way: inserir a descrição da imagem aqui

  • thanks man, I managed to solve the problem! But would have with me change the color of only one column?

Browser other questions tagged

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