TableView cell word wrapping

别来无恙 提交于 2021-02-10 12:58:02

问题


I want to create a cell where i can wrap the text this is my solution what i've got so far:

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);
                }
            }
        };
    });

This one is working but the issue is that i had to hard coded the PREFHEIGHT. I don't really understand why this line is not working:

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

Any help is greatly appreciated.


回答1:


Update

Height should be specified for 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();
    }
}



回答2:


Try to disable fixed cell size for your table:

table.setFixedCellSize(Region.USE_COMPUTED_SIZE);



回答3:


i was having the same problem whit table cell i try your aprouch but whit only a modification and works perfectly, just add the prefix this.setPrefHeight();

here is your code whit this

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);
                }
            }
        };
    });

here is my code butt i dont see much of a diference so i believe that it also have to work for you

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;
});



回答4:


Remove

setPrefHeight(117);

and replace

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

by

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

As a result, the row height will change on the fly.



来源:https://stackoverflow.com/questions/28782975/tableview-cell-word-wrapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!