javafx - 设置 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"));
解决方案
推荐阅读
- r - 更改 R 中的因子标签会改变预测吗?
- android - onActivityResult 从未从小部件启动的活动中调用
- reactjs - Webpack-Dev-Server:编译时出错。防止重新加载。(堆栈:反应)
- wordpress - 努力在 Chrome Inspect 中查找元素 - 如何自定义?
- angular - 如何以角度5从URL(使用blob)下载图像
- node.js - node.js 中的“EIO:i/o 错误,写入”是什么,如何消除它?
- c++ - 该程序允许用户处理循环,只要用户输入一个奇数
- flutter - 将 TextField 下划线颜色更改为渐变
- python-3.x - Matplotlib:使用跟踪保存的动画
- python - TensorFlow 和 keras 得到相同的输出预测误差