如何显示空单元格?
How to show empty cells?
我正在使用javafx的tableview,在我的代码中的某些时刻,会有元素为空,如何处理?有时 idProcesso 和 tempoE 将为空,但是,我想显示 idProcessador。
每一行都是Processor的变量class,但是其中一个变量是另外一个对象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.setCellValueFactory(data ->
new SimpleIntegerProperty(data.getValue().
getProcessoRodando().getId()).asObject());
tempoE.setCellValueFactory(data ->
new SimpleIntegerProperty(data.getValue().
getProcessoRodando().getTempoExecucaoRest()).asObject());
有时 data.getValue().getProcessoRodando()
returns 无效。显然,您不能在空值上调用方法。
最简单的解决方案是采纳 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.select or Bindings.selectString,但它们不可靠,因为它们使用反射,这意味着如果您犯了拼写错误,编译器将不会注意到或标记它。
作为附加说明,您应该启用所有编译器警告并解决它们。然后编译器会警告您,您的 TableColumn 对象应该具有以下类型:
private TableColumn<Processador, Integer> idProcessador;
// ...
idProcessador = new TableColumn<>("ID Processador");
我正在使用javafx的tableview,在我的代码中的某些时刻,会有元素为空,如何处理?有时 idProcesso 和 tempoE 将为空,但是,我想显示 idProcessador。
每一行都是Processor的变量class,但是其中一个变量是另外一个对象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.setCellValueFactory(data ->
new SimpleIntegerProperty(data.getValue().
getProcessoRodando().getId()).asObject());
tempoE.setCellValueFactory(data ->
new SimpleIntegerProperty(data.getValue().
getProcessoRodando().getTempoExecucaoRest()).asObject());
有时 data.getValue().getProcessoRodando()
returns 无效。显然,您不能在空值上调用方法。
最简单的解决方案是采纳 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.select or Bindings.selectString,但它们不可靠,因为它们使用反射,这意味着如果您犯了拼写错误,编译器将不会注意到或标记它。
作为附加说明,您应该启用所有编译器警告并解决它们。然后编译器会警告您,您的 TableColumn 对象应该具有以下类型:
private TableColumn<Processador, Integer> idProcessador;
// ...
idProcessador = new TableColumn<>("ID Processador");