JavaFX的文本字段和属性之间的绑定 [英] JavaFX binding between TextField and a property

查看:3081
本文介绍了JavaFX的文本字段和属性之间的绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您创建一个JavaFX文本字段和属性之间的绑定,那么这个绑定无效的每一次击键,这会导致变化的文字。

If you create a binding between a JavaFX TextField and a property, then this binding is invalidated on every keystroke, which causes a change to the text.

如果您有绑定链的默认行为可能导致的问题,因为在编辑的中间值可能是无效的。

If you have a chain of bindings the default behavior could cause problems, because in the middle of the editing values may be not valid.

好吧,我知道我可以创建一个单向从属性到文本框绑定和注册一个变化监听器当光标离开现场得到信息,如果需要手动更新属性。

Ok, I know I could create an uni-directional binding from the property to the textfield and register a change listener to get informed when the cursor leaves the field and update the property manually if necessary.

有没有更改此行为,使装订只无效的编辑完成,例如,当一个轻松,优雅的方式当光标离开现场?

Is there an easy, elegant way to change this behavior so that the binding is only invalidated when the editing is complete, e.g. when the cursor leaves the field?

感谢

推荐答案

我想你已经pretty多描述做的唯一途径。下面是关于我可以看到它实现最彻底的方法(使用Java 8,虽然它是很容易的lambda表达式转换回是JavaFX的2.2兼容的,如果你需要):

I think you've pretty much described the only way to do it. Here's about the cleanest way I can see to implement it (using Java 8, though it's easy enough to convert the lambdas back to be JavaFX 2.2 compatible if you need):

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.StringBinding;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

    public class CommitBoundTextField extends Application {

        @Override
        public void start(Stage primaryStage) {
            TextField tf1 = new TextField();
            createCommitBinding(tf1).addListener((obs, oldText, newText) -> 
                System.out.printf("Text 1 changed from \"%s\" to \"%s\"%n", oldText, newText));
            TextField tf2 = new TextField();
            createCommitBinding(tf2).addListener((obs, oldText, newText) -> 
                System.out.printf("Text 2 changed from \"%s\" to \"%s\"%n", oldText, newText));
            VBox root = new VBox(5, tf1, tf2);
            Scene scene = new Scene(root, 250, 100);
            primaryStage.setScene(scene);
            primaryStage.show();
        }

        private StringBinding createCommitBinding(TextField textField) {
            StringBinding binding = Bindings.createStringBinding(() -> textField.getText());
            textField.addEventHandler(ActionEvent.ACTION, evt -> binding.invalidate());
            textField.focusedProperty().addListener((obs, wasFocused, isFocused)-> {
                if (! isFocused) binding.invalidate();
            });
            return binding ;
        }

        public static void main(String[] args) {
            launch(args);
        }
    }

这篇关于JavaFX的文本字段和属性之间的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆