JavaFX Data Mangement [duplicate]

北慕城南 提交于 2019-12-11 06:23:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!