首页 > 解决方案 > 设置 TableView 单元格的背景颜色

问题描述

我想在 TableView 中设置单元格的背景颜色。我有一个for循环并添加了 CSS 规则,但我正在更改整个列中的背景颜色。

当单元格值为“ALTA”时,我希望单元格具有红色背景。当值为“MEDIA”时,我想要黄色背景。当单元格值为“BAJA”时,我想要一个绿色背景。

这个循环改变了整个列,我只需要改变单元格的颜色:

for (RespuestaIn o : tablarincidentes.getItems()) {
    String a= tprioridad.getCellData(o);
    if (a.equals("ALTA")) {
        tprioridad.setStyle("-fx-background-color:red"); 
    }
}

这是我的课:

public class RespuestaIn {
    private IntegerProperty IdRincidente;
    private StringProperty ConcecutivoRin;
    private ObjectProperty <LocalDate> fechaprogramada;
    private StringProperty tiempoe;
    private ObjectProperty <LocalDate> fechaejecutada;
    private StringProperty horaSol;
    private StringProperty tiempor;
    private StringProperty tareaejecutada;
    private StringProperty novedades;
    private StringProperty prioridad;
    private StringProperty verificacion;
    private StringProperty concecutivoincidente;
    private StringProperty concecutivoincidenteid;



    public RespuestaIn (Integer IdRincidente,  String ConcecutivoRin, LocalDate fechaprogramada, String tiempoe , LocalDate fechaejecutada , String horaSol ,String tiempor ,String tareaejecutada ,String novedades ,String prioridad ,String verificacion,String concecutivoincidente,String concecutivoincidenteid ) {

         this.IdRincidente=new SimpleIntegerProperty (IdRincidente);
         this.ConcecutivoRin= new SimpleStringProperty(ConcecutivoRin);
         this.fechaprogramada=  new SimpleObjectProperty<LocalDate> (fechaprogramada);
         this.tiempoe=  new SimpleStringProperty (tiempoe);
         this.fechaejecutada=  new SimpleObjectProperty<LocalDate> (fechaejecutada);;
         this.horaSol= new SimpleStringProperty (horaSol);
         this.tiempor= new SimpleStringProperty (tiempor);
         this.tareaejecutada= new SimpleStringProperty (tareaejecutada);
         this.novedades= new SimpleStringProperty (novedades);
         this.prioridad= new SimpleStringProperty (prioridad);
         this.verificacion= new SimpleStringProperty (verificacion);
         this.concecutivoincidente= new SimpleStringProperty (concecutivoincidente);
         this.concecutivoincidenteid= new SimpleStringProperty (concecutivoincidenteid);

    }


    public Integer getIdRincidente() {
        return IdRincidente.get();
    }


    public void setIdRincidente(Integer idRincidente) {
        this.IdRincidente = new SimpleIntegerProperty ();
    }


    public String getConcecutivoRin() {
        return ConcecutivoRin.get();
    }



    public void setConcecutivoRin(StringProperty concecutivoRin) {
        this.ConcecutivoRin = new SimpleStringProperty();
    }

    public LocalDate getFechaprogramada() {
        return fechaprogramada.get();
    }



    public void setFechaprogramada(LocalDate fechaprogramada) {
        this.fechaprogramada = new SimpleObjectProperty<> (fechaprogramada);
    }




    public ObjectProperty<LocalDate> fechaprogramadaProperty() {
        return fechaprogramada;
    }




    public String getTiempoe() {
        return tiempoe.get();
    }



    public void setTiempoe(StringProperty tiempoe) {
        this.tiempoe = new SimpleStringProperty();
    }


    public LocalDate getFechaejecutada() {
        return fechaejecutada.get();
    }



    public void setFechaejecutada(LocalDate fechaejecutada) {
        this.fechaejecutada = new SimpleObjectProperty<> (fechaejecutada);
    }




    public ObjectProperty<LocalDate> fechaejecutadaProperty() {
        return fechaejecutada;
    }


    public String getHoraSol() {
        return horaSol.get();
    }



    public void setHoraSol(StringProperty horaSol) {
        this.horaSol = new SimpleStringProperty();
    }



    public String getTiempor() {
        return tiempor.get();
    }




    public void setTiempor(StringProperty tiempor) {
        this.tiempor = new SimpleStringProperty();
    }




    public String getTareaejecutada() {
        return tareaejecutada.get();
    }



    public void setTareaejecutada(StringProperty tareaejecutada) {
        this.tareaejecutada = new SimpleStringProperty();
    }



    public String getNovedades() {
        return novedades.get();
    }


    public String getConcecutivoincidente() {
        return concecutivoincidente.get();
    }


    public void setConcecutivoincidente(StringProperty concecutivoincidente) {
        this.concecutivoincidente = new SimpleStringProperty();
    }



    public String getConcecutivoincidenteid() {
        return concecutivoincidenteid.get();
    }



    public void setConcecutivoincidenteid(StringProperty concecutivoincidenteid) {
        this.concecutivoincidenteid = new SimpleStringProperty();
    }



    public String getPrioridad() {
        return prioridad.get();
    }



    public void setPrioridad(StringProperty prioridad) {
        this.prioridad = new SimpleStringProperty();
    }



    public String getVerificacion() {
        return verificacion.get();
    }

    public void setVerificacion(StringProperty verificacion) {
        this.verificacion = new SimpleStringProperty();
    }


}

这是 RincidentesController:

@FXML private TableView<RespuestaIn>  tablarincidentes;
@FXML private TableColumn<RespuestaIn, Integer>    idrin;
@FXML private TableColumn<RespuestaIn, String>     concecutivorin;
@FXML private TableColumn<RespuestaIn, LocalDate>  fechapro;
@FXML private TableColumn<RespuestaIn, String>     testimado;
@FXML private TableColumn<RespuestaIn, LocalDate>  fejecutada;
@FXML private TableColumn<RespuestaIn, String > hsolucion;
@FXML private TableColumn<RespuestaIn, String > trespuesta;
@FXML private TableColumn<RespuestaIn, String > tejecutada;
@FXML private TableColumn<RespuestaIn, String > novedades;
@FXML private TableColumn<RespuestaIn, String > tprioridad;
@FXML private TableColumn<RespuestaIn, String > tverificacion;
@FXML private TableColumn<RespuestaIn, String > cnsinc;
@FXML private TableColumn<RespuestaIn, String > cnsincid;


idrin.setCellValueFactory(new PropertyValueFactory <RespuestaIn,Integer>("IdRincidente"));
concecutivorin.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("ConcecutivoRin"));
fechapro.setCellValueFactory(CellData -> CellData.getValue().fechaprogramadaProperty());
testimado.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("tiempoe"));
fejecutada.setCellValueFactory(CellData -> CellData.getValue().fechaejecutadaProperty());
hsolucion.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("horaSol"));
trespuesta.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("tiempor"));
tejecutada.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("tareaejecutada"));
novedades.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("novedades"));
tprioridad.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("prioridad"));
tverificacion.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("verificacion"));
cnsinc.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("concecutivoincidente"));
cnsincid.setCellValueFactory(new PropertyValueFactory <RespuestaIn,String>("concecutivoincidenteid"));

标签: javafx

解决方案


推荐阅读