问题
Scene 1 with its Scene1Controller! it has a text field (Customer Name) and a button!
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
typed "stackoverflow" into the textfield of on-screen keyboard!
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?
SCENE 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="..." />
On-Screen Keyboard:
All the Key's and
Enter Button Code:
<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"/>
Scene 1 Controller:
@FXML
public void ButtonNameClick(final ActionEvent event)
{
//opens on-screen keyboard
}
On-Screen Keyboard Controller:
@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:
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
}
And
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...
}
来源:https://stackoverflow.com/questions/23118330/javafx-data-mangement