How do I change the color of a cell in a jTable? [closed]

0

I've been investigating how to do it, but the only thing I find in creating another class extending DefaultTableCellRenderer but the problem I have is that I already have the code that creates the table done, the question is if I want the bottom of a cell Specify the table with a gray background as it would be, or you must necessarily do so by creating a class by extending DefaultTableCellRenderer. the idea is to change the color using a method something like jTable1.setcolor or something like that. happens that in my code I have to show the major and minor data of the table changing the background color of them then what I want is to change the background of the cell in the table that I get what is the cell after checking what the data is I have to change the color to them

    
asked by brandi003 01.01.2019 в 01:09
source

1 answer

0

You can create a class that is responsible for performing the same as shown in this example:

public class EditorCeldas extends JLabel implements TableCellRenderer{
int Row,Columns;
public void setRow(int Row){
    this.Row=Row;
}
public void setColumns(int Columns){
    this.Columns=Columns;
}
public EditorCeldas() {
    setOpaque(true); // Permite que se vea el color en la celda del JLabel
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    if((row == Row) && (column == Columns)){
    setBackground(Color.red); // Una condicion arbitraria solo para pintar el JLabel que esta en la celda.
    setText(String.valueOf(value)); // Se agrega el valor que viene por defecto en la celda
}
if((row != Row)&& (column==Columns)){
    setBackground(Color.white); // Una condicion arbitraria solo para pintar el JLabel que esta en la celda.
    setText(String.valueOf(value)); // Se agrega el valor que viene por defecto en la celda
}
     }
    return this;
}

}

Then we apply this class to the column of interest in the table

TableColumn columna = jTable1.getColumnModel().getColumn(1);// selecciono la columna que me interesa de la tabla
EditorCeldas TableCellRenderer = new EditorCeldas();
TableCellRenderer.setColumns(column); //se le da por parametro la columna que se quiere modificar
TableCellRenderer.setRow(Row);//se le da por parametro la fila que se quiere modificar
columna.setCellRenderer(TableCellRenderer); // le aplico la edicion
    
answered by 01.01.2019 / 22:10
source