java - 如何在 JavaFX 中绑定(bind) TextField 和 String 对象?

标签 java listview javafx textfield

我在 TextField 中显示了一些单词。每次在 TextField 中更改这些词时,我都想重写这些词。 这是我喜欢的类型:

class WordType {
   String first;
   String second;
}

和我显示我的话的 ListView:

ListView<WordType> list = new ListView<>();

list.setCellFactory(lv -> new ListCell<WordType>() {
        @Override
        public void updateItem(WordType item, boolean empty){
            super.updateItem(item, empty);
            if (empty){
                setText(null);
            } else {
                ToolBar root = new ToolBar();

                TextField word = new TextField(item.first);

                TextField translate = new TextField(item.second);

                root.getItems().addAll(word, new Separator(), translate);
                setGraphic(root);
            }
        }
    });

如果我在 TextField 中更改这些字符串,如何更改 WordType 中的字符串?

对不起,我的英语不好 =)

最佳答案

首先,修改您的 WordType 类以使用 JavaFX 属性:

public class WordType {

    private final StringProperty first = new SimpleStringProperty();
    private final StringProperty second = new SimpleStringProperty();

    public StringProperty firstProperty() {
        return first ;
    }

    public final String getFirst() {
        return firstProperty().get() ;
    }

    public final void setFirst(String first) {
        firstProperty().set(first);
    }


    public StringProperty secondProperty() {
        return second ;
    }

    public final String getSecond() {
        return secondProperty().get() ;
    }

    public final void setSecond(String second) {
        secondProperty().set(second);
    }
}

现在修改列表单元格的实现,使其只创建一次文本字段。当项目发生变化时,将文本字段双向绑定(bind)到新项目中的属性:

list.setCellFactory(lv -> new ListCell<WordType>() {

    private final TextField word = new TextField();
    private final TextField translate = new TextField();
    private final ToolBar root = new ToolBar(word, new Separator(), translate);

    {
        // anonymous constructor:

        itemProperty().addListener((obs, oldWordType, newWordType) -> {
            if (oldWordType != null) {
                word.textProperty().unbindBidirectional(oldWordType.firstProperty());
                translate.textProperty().unbindBidirectional(oldWordType.secondProperty());
            }

            if (newWordType != null) {
                word.textProperty().bindBidirectional(newWordType.firstProperty());
                translate.textProperty().bindBidirectional(newWordType.secondProperty());
            }
        });
    }

    @Override
    protected void updateItem(WordType item, boolean empty) {
        super.updateItem(item, empty);
        setGraphic(empty ? null : root);
    }
});

如果由于某种原因您无法更改 WordType 的实现,那么您可以在文本字段上使用一些监听器。请注意,这只会以一种方式起作用:如果文本字段中的文本发生变化,则 WordType 属性也会发生变化;但是,如果在显示单元格时 WordType 属性从其他来源更改,则单元格将不会更新。根据您的应用程序要求,这可能是也可能不是问题。本例中的单元实现如下所示:

list.setCellFactory(lv -> new ListCell<WordType>() {

    private final TextField word = new TextField();
    private final TextField translate = new TextField();
    private final ToolBar root = new ToolBar(word, new Separator(), translate);

    {
        // anonymous constructor:

        word.textProperty().addListener((obs, oldText, newText) -> {
            WordType wordType = getItem();
            wordType.setFirst(newText);
        });

        translate.textProperty().addListener((obs, oldText, newText) -> {
            WordType wordType = getItem();
            wordType.setSecond(newText);
        });
    }

    @Override
    protected void updateItem(WordType item, boolean empty) {
        super.updateItem(item, empty);
        setGraphic(empty ? null : root);
    }
});

使用您现有的 WordType 类(为简洁起见,我假设您省略了 getset 方法)。

关于java - 如何在 JavaFX 中绑定(bind) TextField 和 String 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40403473/

相关文章:

java - 如何在java swing中提供资源的路径

实现通用接口(interface)的 Java 通用类

java - 使用java将已排序的数据分成组(从A到E,从F到J....)

java - WebView 未从 ListView 按钮加载正确的 URL

wpf - 使用 System.Linq.IGrouping<string,Class T> 对 WPF ListView 进行排序

java - 旋转器事件后 Json Listview 不会刷新

JavaFX:用于十六进制编码内容的自定义 TextArea

java - 如何知道 JavaFX 中的插入符坐标(而不是位置)?

java - JAXB:注释对象列表元素标签名称的简单方法

java - Selenium - 在同一字段中输入的文本,即使打算在不同的字段中输入