【发布时间】:2019-09-27 23:36:17
【问题描述】:
我正在使用来自 javafx 的 tableview,并且在我的代码的某些时刻,会有元素为空,如何处理它?有时 idProcesso 和 tempoE 将为空,但是,我想显示 idProcessador。
每一行都包含Processor类的变量,但是这些变量之一是另一个对象Process,而this有时又可以为null,如何修改使这些行什么都不显示?因为当我要求展示时会给出一个零点
public void initialize(URL location, ResourceBundle resources) {
idProcessador = new TableColumn("ID Processador");
idProcessador.setPrefWidth(100);
idProcessador.setCellValueFactory(data ->
new SimpleIntegerProperty(data.getValue().getId()).asObject());
idProcesso = new TableColumn("ID Processo");
idProcesso.setCellValueFactory(data ->
new SimpleIntegerProperty(data.getValue().
getProcessoRodando().getId()).asObject());
tempoE = new TableColumn("Tempo Execução Restante");
tempoE.setPrefWidth(100);
tempoE.setCellValueFactory(data ->
new SimpleIntegerProperty(data.getValue().
getProcessoRodando().getTempoExecucaoRest()).asObject());
tabProcessador.getColumns().addAll(idProcessador, idProcesso,tempoE);
}
public class Processador {
private Processo processoRodando;
private int id;
private Integer quantum = null;
}
public class Processo implements Comparable<Processo> {
int id ;
int tempoExecucao;
int estadoProcesso;
int tempoExecucaoRest;
}
【问题讨论】:
-
“有时 idProcesso 和 tempoE 将为空”——它们不能为空,因为您已为它们分配了
new TableColumn并且在 Java 中new永远不会返回空值。您的意思是他们的那些列有时在某些行中有空单元格值吗? -
不清楚您遇到了什么问题。
TableCell完全能够容纳null项目,并且通过提供自定义TableColumn#cellFactory返回一个覆盖Cell#updateItem(T,boolean)的自定义TableCell实现,可以完全自定义它的显示方式。查看文档以查看如何覆盖该方法的示例。 -
@VGR 例如,每一行都包含Processor类的变量,但是其中一个变量是另一个对象Process,而this有时又可以为null,如何修改使这些行什么都不显示?因为当我要求展示时会给出一个空点
-
getProcessoRodando()是否为这些行返回 null? -
@VGR 有时,是的