Conditional colors in TableView JavaFX

0

I have a table in javafx like this:

What I need is to change the color of the cells in the KmSopassed column according to the value it has. for example if the value is greater than 5000 it should appear in red if it is smaller yellow etc. here I have the code that fills the table:

  @FXML
private TableView<Panel1> tblvehiculosproxmante;
private ObservableList<Panel1> infoproximos;

@FXML
private TableColumn<Panel1, String> clmnplaca;
@FXML
private TableColumn<Panel1, Number> clmnkmsobretiempo;
private Conexion conexion = new Conexion();
@FXML
private JFXButton btn_report_sobtiemp;


public void initialize(URL url, ResourceBundle rb) {
        conexion = new Conexion();
        conexion.establecerConexion();

    infoproximos = FXCollections.observableArrayList();
    Panel1.mostrarVehiculosProxMante(conexion.getConnection(), infoproximos);
    tblvehiculosproxmante.setItems(infoproximos);

    clmnplaca.setCellValueFactory(
            new PropertyValueFactory<Panel1, String>("placa")
    );
    clmnkmsobretiempo.setCellValueFactory(
            new PropertyValueFactory<Panel1, Number>("kmsobretiempo")
    );
    tblvehiculosproxmante.setItems(infoproximos);
}
    
asked by Riddick 15.11.2017 в 20:21
source

1 answer

0

You can apply it to any type of column. For this case my column is a string but you can convert it into an integer and make the conditional ones you want but in essence it is the same in the whole structure.

public void modificarCelda(TableColumn<Modelo_Mensual, String> cedulas){ // columna de interes
    cedulas.setCellFactory(new Callback<TableColumn<Modelo_Mensual, String>, TableCell<Modelo_Mensual, String>>(){ // cambiar el factor o como se va a comportar la celda
        @Override
        public TableCell<Modelo_Mensual, String> call(TableColumn<Modelo_Mensual, String> param) {

            return new TableCell<Modelo_Mensual, String>(){
                @Override
                protected void updateItem(String item, boolean empty) {//actualizando la celda con los parametros de interes
                    super.updateItem(item, empty); //To change body of generated methods, choose Tools | Templates.

                  if (item != null){ //esta es para asegurarse que haya informacion
                        setStyle("-fx-background-color:#"+item); //y se coloca el estilo de fondo segun el color que mencione la celda
                        //-fx-background-color:#f0f8ff //este es un ejemplo de como debe quedar.
                 }

                }
            };
        }
    });
}

Color code

    
answered by 03.12.2017 / 16:25
source