JavaFx:组合框文本换行

标签 java javafx combobox javafx-8 word-wrap

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

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

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

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

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

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

这是屏幕截图:

enter image description here

最佳答案

我对这个问题有点困惑。您想在编辑或非编辑模式下显示换行的文本。?因为如果您处于编辑模式,您将无法在文本框中看到...。 因此,我假设您要求在从弹出列表中选择后显示换行的文本,并且组合未处于编辑模式。我们可以使用 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);
        }
    }
}

输出如下:

enter image description here

关于JavaFx:组合框文本换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55471652/

相关文章:

java - 如何从组合框值传递到

c# - 组合框自定义项和数据绑定(bind)项

php - php中从MYSQL获取数据时在组合框中选择的属性的作用是什么

java - 如何重置嵌套 while 循环中的值?

java - 如何使用 spring jdbc 存储过程将用户定义的数据类型变量的值从 java 传递到 MS SQL

java - XML 中的 XML 问题要读取 &lt

java - 在 JavaFX 中调整选项卡内容的大小

java - 将文本区域自动调整为 javaFX 中的选项卡 Pane

java - iOS - JAVA 实现实时数据同步的最佳方法

java - 如何在JavaFX中播放声音?