TableView单元格包装

时间:2015-02-28 14:45:11

标签: java javafx javafx-8

我想创建一个我可以包装文本的单元格这是我迄今为止所获得的解决方案:

tableCol.setCellFactory(param -> {
        return new TableCell<Data, String>() {
            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);

                if (item == null || empty) {
                    setText(null);
                    setStyle("");
                } else {
                    Text text = new Text(item);
                    text.setStyle("-fx-padding: 5px 30px 5px 5px;"
                            + "-fx-text-alignment:justify;");
                    text.setWrappingWidth(param.getPrefWidth() - 35);
                    System.out.println(text.getLayoutBounds().getHeight()+10);//117
                    //setPrefHeight(text.getLayoutBounds().getHeight()+10); -----> This is not working somehow
                    setPrefHeight(117);
                    setGraphic(text);
                }
            }
        };
    });

这个是有效的,但问题是我必须对PREFHEIGHT进行硬编码。我真的不明白为什么这条线不起作用:

setPrefHeight(text.getLayoutBounds().getHeight()+10)

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:1)

<强>更新

应为TableRow

指定高度

enter image description here

package com.company;

import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.Region;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        ObservableList<Integer> values = FXCollections.observableArrayList();
        values.addAll(1, 2, 3);


        TableColumn<Integer, Integer> tableColumn = new TableColumn<>("Column1");
        tableColumn.setCellValueFactory(param -> new ReadOnlyObjectWrapper<>(param.getValue()));


        tableColumn.setCellFactory(param -> {
            return new TableCell<Integer, Integer>() {
                @Override
                protected void updateItem(Integer item, boolean empty) {
                    super.updateItem(item, empty);

                    if (item == null) {
                        setText("");
                    } else {
                        setText(item.toString());
                    }
                }
            };
        });


        TableView<Integer> tableView = new TableView<>();
        tableView.setFixedCellSize(Region.USE_COMPUTED_SIZE);
        tableView.getColumns().add(tableColumn);
        tableView.setItems(values);

        tableView.setRowFactory(param -> {
            return new TableRow() {
                @Override
                public void updateIndex(int i) {
                    super.updateIndex(i);

                    setMinHeight(50 * i);
                }
            };
        });

        Scene scene = new Scene(tableView, 320, 480);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

答案 1 :(得分:0)

尝试disable fixed cell size表格:

table.setFixedCellSize(Region.USE_COMPUTED_SIZE);

答案 2 :(得分:0)

我有同样的问题惠特表格我尝试你的aprouch但只有一个修改和完美的工作,只需添加前缀this.setPrefHeight();

这是你的代码

tableCol.setCellFactory(param -> {
        return new TableCell<Data, String>() {
            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);

                if (item == null || empty) {
                    this.setText(null);
                    this.setStyle("");
                } else {
                    Text text = new Text(item);
                    text.setStyle("-fx-padding: 5px 30px 5px 5px;"
                            + "-fx-text-alignment:justify;");
                    text.setWrappingWidth(param.getPrefWidth() - 35);
                    //the only change that i make
                    this.setPrefHeight(text.getLayoutBounds().getHeight()+10);
                    this.setGraphic(text);
                }
            }
        };
    });

这是我的代码,但我没有看到太大的不同,所以我相信它也必须为你工作

colObserva.setCellFactory (col -> {
    TableCell<Procedimientos, String> cell = new TableCell<Procedimientos, String>() {
        @Override
        public void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if (item != null) {
                   Text text = new Text(item);
                   text.setStyle(" -fx-opacity: 1;" +
                                 " -fx-font-family: \"verdena\";" +
                                 " -fx-font-size: 12pt;" +
                                 " -fx-fill: #1398c8;" +   
                                 " -fx-text-wrap: true;" +
                                 " -fx-font-weight: bold;;" +
                                 " -fx-padding: 5px 30px 5px 5px;" +
                                 " -fx-text-alignment:left;");
                   text.setWrappingWidth(col.getPrefWidth() - 35);
                   this.setPrefHeight(text.getLayoutBounds().getHeight()+10);
                   this.setGraphic(text);
            }
        }
    };
    return cell;
});

答案 3 :(得分:0)

删除

setPrefHeight(117);

并替换

text.setWrappingWidth(param.getPrefWidth() - 35);

通过

text.wrappingWidthProperty().bind(getTableColumn().widthProperty().subtract(35));

因此,行高会随时改变。