[在JavaFX中更新TableCell时的奇怪行为

问题描述 投票:0回答:1

[在滚动查看并单击TableView时,我发现了奇怪的行为。

基本上每个带有“ Missing”文本的单元格都应使用红色字体。启动应用程序时,一切正常,并且可以正常工作:Base Case

双击复选框并向上滚动(现在第一行在视图中)后,第一行的字体为红色:First line red

向下滚动后,最后一行的字体为红色:Last line red

这是我正在使用的代码:

    tcAddressAlias.setCellFactory(tc -> new TableCell<Account, String>() {
        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);

            if (!empty) {
                Account a = getTableView().getItems().get(getIndex());

                if(a.getAddress() != null) {
                    setText(item);
                } else {
                    setText("Missing");
                    setStyle("-fx-text-fill: red");
                }
            }
        }
    });

    tcAddressAlias.setCellValueFactory(tc -> 
        Optional.ofNullable(tc.getValue().getAddress())
        .map(add -> new SimpleStringProperty(add.getAlias()))
        .orElseGet(() -> new SimpleStringProperty(""))
    );  

我已经尝试过调试,但是一切似乎都很好...

java javafx tableview
1个回答
0
投票

感谢@michelson!我加了

setStyle(null);

示例:

                if(a.getCreditCard() != null) {
                    setText(item);
                    setStyle(null);
                } else {
                    setText("Missing");
                    setStyle("-fx-text-fill: red");
                }
© www.soinside.com 2019 - 2024. All rights reserved.