TableF中的JavaFx文本换行

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

我在TableCells中的文本换行有问题,如果更改单元格的数据,其中之一就是文本换行不会自动工作,即使我做了一些修改也是如此。我很好奇,如果有人遇到和我一样的问题,也许已经找到了更好的解决方案。

这是一个可以验证的简单示例:

public class Controller implements Initializable {

    @FXML
    private TableView<Model> table;
    @FXML
    private TableColumn<Model, String> colA;
    @FXML
    private TableColumn<Model, String> colB;
    @FXML
    private Button changeString;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        colA.setCellValueFactory(data -> data.getValue().stringA);
        colB.setCellValueFactory(data -> data.getValue().stringB);

        setCellFactory();

        ObservableList<Model> items = FXCollections.observableArrayList();

        items.add(new Model("This is a long string that needs to be wrapped", "Short"));
        items.add(new Model("This is a long string that needs to be wrapped", "Short"));

        changeString.setOnAction(event -> {
            table.getItems().get(0).getStringA().setValue("Short");
            table.getColumns().get(0).setVisible(false);
            table.getColumns().get(0).setVisible(true);
        });

        table.setItems(items);
    }

    private void setCellFactory() {
        colA.setCellFactory(f -> {
            TableCell<Model, String> cell = new TableCell<Model, String>() {
                Text text = new Text();

                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty) {
                        setGraphic(null);
                        return;
                    }
                    text.setWrappingWidth(getTableColumn().getWidth() - 10);
                    text.setText(item);
                    setGraphic(text);
                }
            };
            return cell;
        });
    }

    @Getter
    private static class Model {

        private final StringProperty stringA;
        private final StringProperty stringB;

        private Model(String stringA, String stringB) {
            this.stringA = new SimpleStringProperty(stringA);
            this.stringB = new SimpleStringProperty(stringB);
        }
    }
}

(这只是相关的部分,如果需要,我将包括Mainfxml

如您所见,我必须在显示在表格单元格中的模型中设置一个值,但是在设置之后,该单元格的高度不会更新。我要对其进行更新,我必须包括该列的hide / show hack以刷新高度。

您对此问题有更好的建议/解决方案吗?

javafx javafx-8 word-wrap
1个回答
0
投票

这与setCellFactory中的updateItem在主线程中有关。像这样更改您的单元工厂以使其在FX线程中运行

                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    Platform.runLater(() -> {
                        if (empty) {
                            setGraphic(null);
                        } else {
                            text.wrappingWidthProperty().bind(getTableColumn().widthProperty().subtract(10));
                            text.setText(item);
                            setGraphic(text);
                        }
                    });

                }
            };
        });
    }

并且您可以在changeString.setOnAction事件中删除可见的列hack

或者,由于认为runLater是一个hack,请使用tableview刷新...仅使用原始代码片段,而不是

       changeString.setOnAction(event -> {
            table.getItems().get(0).getStringA().setValue("Short");
            table.getColumns().get(0).setVisible(false);
            table.getColumns().get(0).setVisible(true);
        });

替换为

       changeString.setOnAction(event -> {
            table.getItems().get(0).getStringA().setValue("Short");
            table.refresh();
        });
© www.soinside.com 2019 - 2024. All rights reserved.