简体   繁体   English

实时JavaFX-8表单管理

[英]Real-time JavaFX-8 Form Management

Alright, here's my dilemma. 好吧,这是我的难题。 I'm trying to create a JavaFX Form with TextFields and other elements. 我正在尝试使用TextFields和其他元素创建JavaFX Form。 But for right now, I need to know how to implement a form that fills in realtime. 但是现在,我需要知道如何实现实时填写的表单。

Theoretically, for example let's say I have two TextFields. 从理论上讲,例如,我有两个TextField。

TextField textfield1 = new TextField();
TextField textfield2 = new TextField();

The second textfield depends on whatever value was entered in the first textfield. 第二个文本字段取决于在第一个文本字段中输入的任何值。 As soon as the user has finished typing on the first, the second is then populated with a value. 用户在第一个键盘上完成键入后,立即在第二个键盘中填充一个值。

if(!textfield.getText().equals("")) {
    textfield2.setText("blah, blah, blah");
}

For a real case example, let's say that I was making a payroll system. 举一个真实的例子,假设我正在创建一个工资系统。 The user types in the employee's basic pay (in the first textfield), as soon as that's done the value for the employee's monthly salary: basic pay * 2, is entered (in the second textfield). 用户输入完员工的基本工资(在第一个文本字段中)后,便立即输入了员工的月薪值:基本工资* 2(在第二个文本字段中)。

The second is non-editable, only the first one is. 第二个是不可编辑的,只有第一个是不可编辑的。 Pretty sure that I need some kind of event handler on the first field, but I really am stumped on how or what code to write. 可以肯定的是,我在第一个字段上需要某种事件处理程序,但我确实对如何编写代码或编写什么代码感到困惑。 I am new to JavaFX by the way. 顺便说一下,我是JavaFX的新手。 Any help would be appreciated. 任何帮助,将不胜感激。

UPDATE : I'm using Scenebuilder on this (sorry I forgot to include that). 更新 :我正在对此使用Scenebuilder(对不起,我忘了包含它)。 Because of that each elements' listeners are grouped into defined functions in the controller. 因此,每个元素的侦听器都在控制器中分组为已定义的功能。 It feels weird to me using inner classes ever since I scrapped Swing (and Windowbuilder), using Eclipse by the way. 自从我使用Eclipse废弃Swing(和Windowbuilder)以来,使用内部类对我来说感觉很奇怪。 I'd like to keep the code convention consistent all across the board. 我想使代码约定在所有方面保持一致。

I believe you're asking how to implement a focus lost listener. 我相信您在问如何实现焦点丢失的监听器。 This demonstrates how you can add a change listener to the focusProperty() of the first text field and use it to trigger an update of the second. 这演示了如何将更改侦听器添加到第一个文本字段的focusProperty()并使用它来触发第二个文本字段的更新。

This can be done within the controller by placing logic inside the an @FXML annotated initialize() method. 可以在控制器中通过将逻辑放在@FXML注释的initialize()方法中来完成。 This is called automagically by JavaFX after all the @FXML annotated fields have been injected. 注入所有@FXML带注释的字段后,JavaFX会自动对其进行调用。

public class Controller {
    @FXML
    private TextField textfield1;
    @FXML
    private TextField textfield2;

    @FXML
    public void initialize() {
        textfield1.focusedProperty().addListener((obs, oldValue, newValue) -> {
            if (!newValue) { // focus lost
                if (!"".equals(textfield1.getText())) {
                    textfield2.setText("blah, blah, blah");
                } else {
                    textfield2.setText("");
                }
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM