JavaFX数据管理 [英] JavaFX Data Mangement

查看:164
本文介绍了JavaFX数据管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景1及其Scene1Controller!它有一个文本字段(客户名称)和一个按钮!

Scene 1 with its Scene1Controller! it has a text field (Customer Name) and a button!

当我点击场景1中的按钮时,会出现一个屏幕键盘而不关闭场景!

When I click the button in scene 1, a on-screen keyboard will appear without closing the scene!

屏幕键盘上有它自己的控制器!

on-screen keyboard has it has its own controller!

屏幕键盘有一个文本字段并且完整键盘

on-screen keyboard has a textfield and complete keyboard

键入stackoverflow进入屏幕键盘的文本字段!

typed "stackoverflow" into the textfield of on-screen keyboard!

按下输入后屏幕键盘如何将屏幕键盘的文本字段值检索到场景1的客户名称字段?

after pressing enter in the on-screen keyboard how do I retrieve the textfield value of the on-screen keyboard into the customer name field of scene 1?

场景1:

<TextField fx:id="CustomerName" layoutX="14.0" layoutY="75.0" onAction="#TextBoxTextChanged" prefHeight="29.0" prefWidth="254.0"/>
<Button fx:id="OnScreenKeyBoardButton" layoutX="268.0" layoutY="75.0" mnemonicParsing="false" onAction="#ButtonNameClick" prefHeight="29.0" text="..." />

屏幕键盘:

全部密钥和

输入按钮代码:

<Button fx:id="enterButton" layoutX="796.0" layoutY="210.0" minHeight="18.8" mnemonicParsing="false" prefHeight="40.0" prefWidth="90.0" text="Enter" onAction="#ButtonEnterClick"/>

场景1控制器:

@FXML
public void ButtonNameClick(final ActionEvent event)
{
  //opens on-screen keyboard
}

屏幕键盘控制器:

@FXML
public void ButtonEnterClick(final ActionEvent event)
{
  //code to be written to get the text field of the on-screen keyboard into the textfield of scene 1
}


推荐答案

只需在键盘控制器中创建一个属性即可代表文本,并从Screen1Controller中观察它:

Just create a property in the keyboard controller to represent the text, and observe it from the "Screen1Controller":

public class KeyboardController {
    private StringProperty text = new SimpleStringProperty(this, "text", "");
    public StringProperty textProperty() {
         return text ;
    }
    public String getText() {
        return text.get();
    }
    public void setText(String text) {
        this.text.set(text);
    }

    @FXML
    public void buttonEnterClick(ActionEvent event) {
        text.set(// text from keyboard) ;
    }
    // ... everything else as before
}

public class Screen1Controller {
    @FXML
    private TextField customerName ;
    // ...
    @FXML
    public void buttonNameClick(ActionEvent event) {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Keyboard.fxml"));
        Parent parent = loader.load();
        KeyboardController controller = (KeyboardContoller) loader.getController();
        controller.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> obs, String oldValue, String newValue) {
                // update text field with newValue:
                customerName.setText(newValue);
            }
        });
        // show keyboard ...
    }

    // other code...
}

这篇关于JavaFX数据管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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