JavaFX TableView显示空值

时间:2017-10-10 16:32:26

标签: java javafx filter null

我最近遇到了有趣的问题。我有一个包含1个int和3个String列的表。我已经为表格实现了完全正常的过滤,除了一个小点:每当至少有一个过滤结果(但小于可见行的数量)时,int列显示空值作为空行的值。但是,如果找到的匹配数量大于可见行数,则不会添加空值(即使使用滚动功能)。最好用图片描述:

with null values

筛选不存在的值不显示空值:

without null values

过滤代码:

    FilteredList<Word> filteredData = new FilteredList<>(masterData,e->true);
    SortedList<Word> sortedData = new SortedList<>(filteredData);
    sortedData.comparatorProperty().bind(table.comparatorProperty());
    table.setItems(sortedData);
    TextField filter = new TextField();
    filter.setPromptText("Filter");
    filter.textProperty().addListener((observableValue,oldValue,newValue)->{
        filteredData.setPredicate((Predicate<? super Word>) word->{
            if(word.getAllCz().toLowerCase().contains(newValue.toLowerCase()))return true;
            else if(word.getAllEng().toLowerCase().contains(newValue.toLowerCase()))return true;
            else if(String.valueOf(word.getUnitNo()).equals(newValue))return true;
            else return false;
        });
    });

CellValue工厂:

    column.setCellValueFactory(new PropertyValueFactory<>(data));
    column.setCellFactory(tc-> {
        TableCell<Word, Integer> cell = new TableCell<>();
        Text text = new Text();
        cell.setGraphic(text);
        text.setTextAlignment(TextAlignment.CENTER);
        text.setStyle("-fx-fill: -fx-text-background-color;");
        text.setFontSmoothingType(FontSmoothingType.LCD);
        text.wrappingWidthProperty().bind(column.widthProperty().subtract(5));
        text.textProperty().bind(cell.itemProperty().asString());
        return cell;  
    });

1 个答案:

答案 0 :(得分:1)

如果单元格为空,则其项目为nullitemProperty().asString()将评估为包含文字词的字符串&#34; null&#34; (类似于将空值传递给PrintStream)。您的绑定需要将空单元格视为特殊情况:

column.setCellFactory(tc-> {
    TableCell<Word, Integer> cell = new TableCell<>();
    Text text = new Text();
    cell.setGraphic(text);
    text.setTextAlignment(TextAlignment.CENTER);
    text.setStyle("-fx-fill: -fx-text-background-color;");
    text.setFontSmoothingType(FontSmoothingType.LCD);
    text.wrappingWidthProperty().bind(column.widthProperty().subtract(5));
    text.textProperty().bind(Bindings.createStringBinding(() -> {
        if (cell.isEmpty()) {
            return null ;
        } else {
            return cell.getItem().toString();
        }
    }, cell.emptyProperty(), cell.itemProperty()));
    return cell;  
});

或者您需要覆盖updateItem()

column.setCellFactory(tc-> {
    TableCell<Word, Integer> cell = new TableCell<>() {
        private Text text = new Text();
        {
            this.setGraphic(text);
            text.setTextAlignment(TextAlignment.CENTER);
            text.setStyle("-fx-fill: -fx-text-background-color;");
            text.setFontSmoothingType(FontSmoothingType.LCD);
            text.wrappingWidthProperty().bind(column.widthProperty().subtract(5));
        }

        @Override
        protected void updateItem(Integer item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                text.setText(null);
            } else {
                text.setText(item.toString());
            }
        }
    };
    return cell;  
});
相关问题