Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在JavaFX中绑定TextField和String对象?_Java_Listview_Javafx_Textfield - Fatal编程技术网

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

如何在JavaFX中绑定TextField和String对象?,java,listview,javafx,textfield,Java,Listview,Javafx,Textfield,我在文本字段中显示了一些单词。每次在TextField中更改这些单词时,我都要重写这些单词。 这是我喜欢的类型: class WordType { String first; String second; } 和ListView,我在其中显示我的文字: ListView<WordType> list = new ListView<>(); list.setCellFactory(lv -> new ListCell<WordType>()

我在文本字段中显示了一些单词。每次在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);
            }
        }
    });
ListView列表=新建ListView();
list.setCellFactory(lv->new ListCell(){
@凌驾
public void updateItem(字类型项,布尔空){
super.updateItem(项,空);
if(空){
setText(空);
}否则{
工具栏根=新工具栏();
TextField word=新的TextField(项目。第一);
TextField translate=新的TextField(item.second);
root.getItems().addAll(word,新分隔符(),translate);
设定图形(根);
}
}
});
如果我在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);
    }
}
现在修改列表单元格实现,使其只创建一次文本字段。项目更改时,将文本字段双向绑定到新项目中的属性:

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
类(为了简洁起见,我假设您省略了
get
set
方法)。

您可以将
WordType
类更改为使用JavaFX属性吗?不,我不能。我从一些资源中加载单词,我必须在这些资源中重写我的单词。我不明白为什么这会阻止您更改类实现…?如果在WordType中我将有TextField表单,我如何在资源中重写我的单词?我没有在
WordType
中说使用
TextField
s。我的意思是用字符串而不是普通字符串来表示数据。
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);
    }
});