首页 文章

JavaFX刷新动态着色的tableview

提问于
浏览
-1

我正在使用TableView构建JavaFX应用程序,我希望它根据不同的用户选择动态更改颜色 . 我最初可以使用彩色数据填充它,但刷新它会保留原始颜色 .

这是我的方法的基本功能,每次用户更改时动态调用:

SessionsTableView.getItems().clear();
ObservableList<SessionItem> newsessionitems = FXCollections.observableArrayList();
for (SessionPart x : AllSessionParts) {
    tableitems.add(new SessionItem(count, x.name, x.getdurationasString(true, 150.0), getambiencetext(x), x.goals_getCurrentAsString(true, 150.0)));
    newsessionitems.add(x);
    count++;   
}
SessionsTableView.setItems(newsessionitems);

返回不同的String值,这些值在我的单元格值工厂中测试以确定颜色:

DurationColumn.setCellFactory(new Callback<TableColumn<SessionItem, String>, TableCell<SessionItem, String>>() {
                @Override
                public TableCell<SessionItem, String> call(TableColumn<SessionItem, String> param) {
                    return new TableCell<SessionItem, String>() {
                        @Override
                        protected void updateItem(String item, boolean empty) {
                            super.updateItem(item, empty);
                            if (! isEmpty()) {
                                switch (item) {
                                    case "No Duration Set":
                                        setTextFill(Color.RED);
                                        break;
                                    case "Short Duration Set":
                                        setTextFill(Color.YELLOW);
                                        break;
                                    default:
                                        setTextFill(Color.BLACK);
                                        break;
                                }
                                setText(item);
                            }
                        }
                    };
                }
            });

JavaFX根据初始填充时提供的字符串值为文本着色,但在再次调用时则不会 . 我调用了updateitem(String,boolean)方法的覆盖,并且更改了文本值,但是更改/刷新了更改文本的颜色 .

如何让颜色动态变化?

1 回答

  • 0

    我想到了 . 我没有使用对我不起作用的setTextFill()方法,而是使用了setStyle(“ - fx-text-fill:”) . 这似乎解决了这个问题 . 该表现在按照我的意图动态刷新 .

    DurationColumn.setCellFactory(new Callback<TableColumn<SessionItem, String>, TableCell<SessionItem, String>>() {
                @Override
                public TableCell<SessionItem, String> call(TableColumn<SessionItem, String> param) {
                    return new TableCell<SessionItem, String>() {
                        @Override
                        protected void updateItem(String item, boolean empty) {
                            super.updateItem(item, empty);
                            if (! isEmpty()) {
                                switch (item) {
                                    case "No Duration Set":
                                        setTextFill(Color.RED); // Doesn't Work
                                        setStyle("-fx-text-fill: red") // Does Work 
                                        break;
    
                                }
                                setText(item);
                            }
                        }
                    };
                }
            });
    

相关问题