JavaFx:ComboBox文本包装

JavaFx:ComboBox文本包装,java,javafx,combobox,javafx-8,word-wrap,Java,Javafx,Combobox,Javafx 8,Word Wrap,我试图将文本包装在组合框中,但我无法真正管理它 我需要的是编辑器部分的包装文本,而不是组合框的下拉部分。我看到编辑器是一个TextField,它的文本不能真正被包装,或者 如果我有一个很长的文本,我每次都能看到它,那么有没有什么技巧或解决方案可以让它包装得很好呢 因此,我希望显示整个文本,而不是显示… 代码部分我不知道是否应该添加,因为它实际上是一个简单的表格单元格,一个组合框设置为图形,但如果有帮助,我将编辑问题 注意:将列设置得更宽,以便文本可以容纳,这不是一个解决方案 以下是一个屏幕截图:

我试图将文本包装在组合框中,但我无法真正管理它

我需要的是编辑器部分的包装文本,而不是组合框的下拉部分。我看到编辑器是一个
TextField
,它的文本不能真正被包装,或者

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

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

代码部分我不知道是否应该添加,因为它实际上是一个简单的表格单元格,一个组合框设置为图形,但如果有帮助,我将编辑问题

注意:将列设置得更宽,以便文本可以容纳,这不是一个解决方案

以下是一个屏幕截图:


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

如果这不是你想要的,那么a可以帮助我调查实际问题

请参考下面的代码,了解如何显示包装文本

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);
        }
    }
}
导入javafx.application.application;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.geometry.Insets;
导入javafx.scene.scene;
导入javafx.scene.control.*;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.layout.StackPane;
导入javafx.scene.layout.VBox;
导入javafx.scene.text.Font;
导入javafx.scene.text.text;
导入javafx.stage.stage;
导入javafx.util.Callback;
公共类TableViewComboxCell扩展应用程序{
private TableView table=new TableView();
私有最终可观察列表数据=FXCollections
.可观察的可追踪名单(
新人(“雅各布”、“史密斯”、“雅各布”。smith@example.com"),
新人(“伊莎贝拉”、“约翰逊”,
“伊莎贝拉。johnson@example.com"),
新人(“伊桑”、“威廉姆斯”,
“伊桑。williams@example.com"),
新人(“艾玛”、“琼斯”、“艾玛”。jones@example.com"),
新人(“迈克尔”、“布朗”、“迈克尔”。brown@example.com"));
公共静态void main(字符串[]args){
发射(args);
}
private final ObservableList comboList=FXCollections.observableArrayList(“检查文本换行的第一个包含很长文本的大句子”,
“第二个包含很长文本的大句子,用于检查文本换行”,
“第三个包含很长文本的大句子,用于检查文本换行”,
“第四个大句子,用很长的文字检查文字包装”);
@凌驾
公众假期开始(阶段){
对于(int i=0;i<50;i++){
如果(i%5==0){
添加(新人物(“姓名”+i,“最后”+i,“邮件”+i,comboList.get(0)));
}否则{
数据。添加(新人员(“姓名”+i,“最后”+i,“邮件”+i));
}
}
场景=新场景(新StackPane());
stage.setTitle(“表格视图示例”);
舞台设置宽度(650);
舞台设置高度(500);
最终标签=新标签(“地址簿”);
label.setFont(新字体(“Arial”,20));
TableColumn firstNameCol=新的TableColumn(“名字”);
firstNameCol.setMinWidth(100);
第一名
.setCellValueFactory(新属性ValueFactory(
"名);;
TableColumn lastNameCol=新的TableColumn(“姓氏”);
lastNameCol.setMinWidth(100);
lastNameCol
.setCellValueFactory(新属性ValueFactory(
“姓氏”);
TableColumn emailCol=新的TableColumn(“电子邮件”);
设置最小宽度(200);
emailCol.setCellValueFactory(新属性ValueFactory(
"电邮);;
TableColumn comboCol=新的TableColumn(“组合”);
comboCol.setMinWidth(200);
comboCol.setCellValueFactory(新属性ValueFactory(
"组合"),;
comboCol.setCellFactory(新回调(){
@凌驾
公共TableCell调用(TableColumn参数){
返回新的TableCell(){
私人组合框组合;
@凌驾
受保护的void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
getCombo().getSelectionModel().clearSelection();
如果(!空){
getCombo().setValue(项);
setGraphic(getCombo());
}否则{
设置图形(空);
}
}
私有组合框getCombo(){
if(combo==null){
combo=新的组合框();
combo.setItems(组合列表);
combo.getSelectionModel().SelectEditeProperty().addListener((obs、old、newVal)->{
((Person)getTableRow().getItem()).setCombo(newVal);
});
combo.setButtonCell(新的ListCell(){
私有文本文本;
@凌驾
受保护的void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
设置图形(空);
如果(!空){
getTextLbl