JavaFX数据管理

时间:2014-04-16 19:20:48

标签: javafx fxml

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

单击场景1中的按钮时,将出现屏幕键盘而不关闭场景!

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

屏幕键盘有一个文本字段和完整的键盘

在屏幕键盘的文本字段中键入“stackoverflow”!

在屏幕键盘输入后,如何将屏幕键盘的文本字段值检索到场景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
}

1 个答案:

答案 0 :(得分:3)

只需在键盘控制器中创建一个属性来表示文本,然后从“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...
}