JavaFx TableView 文本换行

TableView 的TableColumn有文本换行的需求。

方法如下:

    @FXML
    private TableColumn nameCol;

    nameCol.setCellValueFactory(new PropertyValueFactory<>("name")); //姓名  初始化nameCol

   //设置nameCol中的文本可换行
    nameCol.setCellFactory(tc -> {

    TableCell<StaffModel,String> cell = new TableCell<>();
    Text text = new Text();
    cell.setGraphic(text);
    cell.setPrefHeight(Control.USE_COMPUTED_SIZE);
    text.wrappingWidthProperty().bind(nameCol.widthProperty());
    text.textProperty().bind(cell.itemProperty());
    return cell;
});

参考

原文地址:https://www.cnblogs.com/yiyezhouming/p/14806133.html