问题描述:

Can I bind stage.getUserData with TextBox?

My goal is.

If user select my application I save information in method start stage.setUserData in event focusesProperty.

public void start(Stage stage) throws Exception {

try{

Parent root = FXMLLoader.load(getClass().getResource("Hlavni.fxml"));

Scene scene = new Scene(root);

stage.setScene(scene);

stage.getIcons().add(new Image("file:kcl.gif"));

stage.setTitle("KCL - Evidence hovorů - v1.0");

stage.show();

/*

Implementujeme událost - stav kdy uživatel vybere okno aplikace.

*/

stage.focusedProperty().addListener((ObservableValue<? extends Boolean> ov, Boolean lostFocus, Boolean gainFocus) -> {

if (gainFocus==true) {

try {

/*

Po výběru gainFocus naplníme systémovou metodu stage.setUserData

V kontroleru dané vyzvědneme přes zjištěnou stage.getUserData

Do metody ukládáme, pouze pokud je null

*/

// Pokud je stage.getUserData == null, uložíme tam číslo, jinak nic.

if(stage.getUserData() == null){

// Připojíme se do MSSQL a zjistíme telefonní číslo

stage.setUserData("607700962");

}

}catch(Exception e){

e.printStackTrace();

}

}

});

In controller I need to fill automatically TextBox.

There for I have idea bind stage.getUserData with TextBox.

网友答案:

I would just put a method in the controller class and call it:

e.g.

public class MyController {

    // @FXML-annotated nodes from fxml file, e.g.
    @FXML
    private TextField textField ;

    public void initialize() {
        // ...
    }

    // handlers etc...

    public void setInformation(String data) {
        // update text box:
        textField.setText(data);
    }
}

and then you can just do

public void start(Stage stage) throws Exception {       

    try{

        FXMLLoader loader = new FXMLLoader(getClass().getResource("Hlavni.fxml"));
        Parent root = loader.load(); 
        MyController controller = loader.getController();                               
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.getIcons().add(new Image("file:kcl.gif"));
        stage.setTitle("KCL - Evidence hovorů - v1.0");                                                                          
        stage.show();



        stage.focusedProperty().addListener((ObservableValue<? extends Boolean> ov, Boolean lostFocus, Boolean gainFocus) -> {
            if (gainFocus) {
                controller.setInformation("607700962");
            }
        });   

    } catch (Exception exc) {
        exc.printStackTrace();
    }
    // ...
}
相关阅读:
Top