JavaFx:组合框 textWrap

JavaFx: ComboBox textWrap

我正在尝试将文本包装在 ComboBox 中,但我无法真正处理它。

我需要编辑器部分的换行文本,而不是 comboBox 的下拉部分。我看到编辑器是一个TextField,它的文字真的不能换行or?

有没有什么技巧或解决方案可以让文本包装得很好,这样如果我的文本很长,我每次都能看到它?

因此,我不想显示 ...,而是希望显示整个文本。

代码部分我不知道是否应该添加,因为它非常简单 table 单元格,ComboBox 设置为 Graphics,但如果有帮助我会编辑问题。

注意:将列设置得更宽以使文本适合,这不是解决方案!

这是截图:

我对这个问题有点困惑。您想在编辑时或在非编辑模式下显示换行的文本。?因为如果您处于编辑模式,则无法在文本框中看到 ...。 因此,我假设您要求在从弹出列表中选择后显示换行文本,并且组合未处于编辑状态 mode.We 可以使用 buttonCell 解决此问题。

如果这不是您要的,那么 Minimal, Complete, and Verifiable example 可以帮助我调查实际问题。

关于如何显示换行的文字,请参考下面的代码。

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Callback;

public class TableViewComboBoxCell extends Application {

    private TableView<Person> table = new TableView<Person>();

    private final ObservableList<Person> data = FXCollections
            .observableArrayList(
                    new Person("Jacob", "Smith", "jacob.smith@example.com"),
                    new Person("Isabella", "Johnson",
                            "isabella.johnson@example.com"),
                    new Person("Ethan", "Williams",
                            "ethan.williams@example.com"),
                    new Person("Emma", "Jones", "emma.jones@example.com"),
                    new Person("Michael", "Brown", "michael.brown@example.com"));


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

    private final ObservableList<String> comboList = FXCollections.observableArrayList("First big sentence with very long text to check the text wrap",
            "Second big sentence with very long text to check the text wrap",
            "Third big sentence with very long text to check the text wrap",
            "Fourth big sentence with very long text to check the text wrap");

    @Override
    public void start(Stage stage) {
        for (int i = 0; i < 50; i++) {
            if(i%5==0){
                data.add(new Person("Name " + i, "Last " + i, "Mail " + i, comboList.get(0)));
            }else {
                data.add(new Person("Name " + i, "Last " + i, "Mail " + i));
            }
        }
        Scene scene = new Scene(new StackPane());
        stage.setTitle("Table View Sample");
        stage.setWidth(650);
        stage.setHeight(500);

        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));

        TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol
                .setCellValueFactory(new PropertyValueFactory<Person, String>(
                        "firstName"));

        TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol
                .setCellValueFactory(new PropertyValueFactory<Person, String>(
                        "lastName"));

        TableColumn<Person, String> emailCol = new TableColumn<>("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>(
                "email"));

        TableColumn<Person, String> comboCol = new TableColumn<>("Combo");
        comboCol.setMinWidth(200);
        comboCol.setCellValueFactory(new PropertyValueFactory<Person, String>(
                "combo"));
        comboCol.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
            @Override
            public TableCell<Person, String> call(TableColumn<Person, String> param) {
                return new TableCell<Person, String>() {
                    private ComboBox<String> combo;

                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        getCombo().getSelectionModel().clearSelection();
                        if (!empty) {
                            getCombo().setValue(item);
                            setGraphic(getCombo());
                        } else {
                            setGraphic(null);
                        }
                    }

                    private ComboBox<String> getCombo() {
                        if (combo == null) {
                            combo = new ComboBox<>();
                            combo.setItems(comboList);
                            combo.getSelectionModel().selectedItemProperty().addListener((obs, old, newVal) -> {
                                ((Person) getTableRow().getItem()).setCombo(newVal);
                            });
                            combo.setButtonCell(new ListCell<String>() {
                                private Text textLbl;
                                @Override
                                protected void updateItem(String item, boolean empty) {
                                    super.updateItem(item, empty);
                                    setGraphic(null);
                                    if (!empty) {
                                        getTextLbl().setText(item);
                                        setGraphic(getTextLbl());
                                    }
                                }

                                private Text getTextLbl(){
                                    if(textLbl ==null){
                                        textLbl = new Text();
                                        textLbl.wrappingWidthProperty().bind(this.widthProperty().subtract(10));
                                    }
                                    return textLbl;
                                }
                            });
                        }
                        return combo;
                    }
                };
            }
        });
        table.setItems(data);
        table.getColumns().addAll(firstNameCol, lastNameCol, emailCol, comboCol);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(label, table);
        ((StackPane) scene.getRoot()).getChildren().addAll(vbox);
        stage.setScene(scene);
        stage.show();
    }

    public static class Person {
        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;
        private final SimpleStringProperty email;
        private final SimpleStringProperty combo;

        private Person(String fName, String lName, String email) {
            this(fName,lName,email,null);
        }

        private Person(String fName, String lName, String email, String comboStr) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
            this.email = new SimpleStringProperty(email);
            this.combo = new SimpleStringProperty(comboStr);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public SimpleStringProperty firstNameProperty() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName.set(firstName);
        }

        public String getLastName() {
            return lastName.get();
        }

        public SimpleStringProperty lastNameProperty() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName.set(lastName);
        }

        public String getEmail() {
            return email.get();
        }

        public SimpleStringProperty emailProperty() {
            return email;
        }

        public void setEmail(String email) {
            this.email.set(email);
        }

        public String getCombo() {
            return combo.get();
        }

        public SimpleStringProperty comboProperty() {
            return combo;
        }

        public void setCombo(String combo) {
            this.combo.set(combo);
        }
    }
}

输出如下: