【问题标题】:How to show empty cells?如何显示空单元格?
【发布时间】: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 有时,是的

标签: java javafx tableview


【解决方案1】:

看来你的问题是这两行:

idProcesso.setCellValueFactory(data -> 
        new SimpleIntegerProperty(data.getValue().
                    getProcessoRodando().getId()).asObject());

tempoE.setCellValueFactory(data -> 
        new SimpleIntegerProperty(data.getValue().
                    getProcessoRodando().getTempoExecucaoRest()).asObject());

有时data.getValue().getProcessoRodando() 返回空值。显然,您不能在 null 值上调用方法。

最简单的解决方案是采纳What is a NullPointerException, and how do I fix it? 中给出的建议,避免在空值上调用方法:

idProcesso.setCellValueFactory(data -> {
        Processo processo = data.getValue().getProcessoRodando();
        return new SimpleObjectProperty<Integer>(processo != null ?
            Integer.valueOf(processo.getId()) : null);
    });

tempoE.setCellValueFactory(data -> {
        Processo processo = data.getValue().getProcessoRodando();
        return new SimpleObjectProperty<Integer>(processo != null ?
            Integer.valueOf(processo.getTempoExecucaoRest()) : null);
    });

您也可以使用 Bindings.selectBindings.selectString,但它们使用反射并不可靠,这意味着如果您犯了拼写错误,编译器不会注意到或标记它。

作为附加说明,您应该启用所有编译器警告并解决它们。然后编译器会警告你 TableColumn 对象应该有类型:

private TableColumn<Processador, Integer> idProcessador;

// ...

idProcessador = new TableColumn<>("ID Processador");

【讨论】:

  • 非常感谢,对不起我的英语,但你理解得很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
  • 1970-01-01
  • 2011-07-03
  • 2021-01-08
相关资源
最近更新 更多