有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在JavaFX中更新TableCell时的java异常行为

在我的TableView中滚动和单击时,我注意到一种奇怪的行为

基本上,每个文本“缺失”的单元格都应该有红色字体。启动应用程序时,一切正常: 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(""))
    );  

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


共 (1) 个答案

  1. # 1 楼答案

    谢谢@michelson! 我补充说

    setStyle(null);
    

    例如:

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