如何在JavaFX中绑定TextField和String对象?

时间:2016-11-03 13:59:01

标签: 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中的字符串?

抱歉,我的英文不好=)

1 个答案:

答案 0 :(得分:3)

首先,修改您的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);
    }
}

现在修改列表单元格实现,使其仅创建一次文本字段。当项目更改时,双向将文本字段绑定到新项目中的属性:

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方法。