How to pass arguments in JavaFX

和自甴很熟 提交于 2020-03-26 11:03:03

问题


So, it's a little confusing but here is the thing:

I created a window with SceneBuilder, and created the controller and everything, there is a button in the window. The button setOnAction() method makes the program open another window, the thing is, this other window is another class and I want to pass information to this other window, but it seems I cannot. Here's some code example:

MainWindow:

confirm.setOnAction(event->{
    try {
        LibraryWindowController lwc = new LibraryWindowController();
        lwc.setDay(day.getValue());
        lwc.setMonth(month.getValue());
        lwc.setYear(Integer.parseInt(year.getText()));
        lwc.setClient(login.getText());
        lwc.start(new Stage());
    } catch (Exception e) {
        System.out.println("An error has occured!");
    }
});

LibraryWindowController (variables and setters):

private int day, month, year;
private String client;

public void setDay(int day){
    this.day = day;
}

public void setMonth(int month){
    this.month = month;
}

public void setYear(int year){
    this.year = year;
}

public void setClient(String login){
    this.client = login;
}

The thing is, inside the Controller's initialize method there is a System.out.println(client); and the problem is that the output is null, so, why is that happening and how to fix it?


回答1:


I recommend you use the fx:root construct to prevent issues like this. I find it helps greatly with structuring your app. It is described in the Custom Components section of the FXML docs. If you use this approach, all your screens/panes are simple subclasses of some Pane class in JavaFX. And because the FXML loading is encapsulated in the class, you can simply use the constructor to create new instances (see the example in the documentation I linked to) and set them up as you would any object.

For example (assume MyPane is a Pane you designed using this approach):

MyPane mp = new MyPane();
mp.setWhatever(whatever);
Stage newWindow = new Stage();
newWindow.setScene(new Scene(mp));
newWindow.show();

All you have to change in SceneBuilder is remove your controller class (you no longer need it, class MyPane itself becomes the controller) and check the "Use fx:root" checkbox.

For a complete example of how to use fx:root, see this example I made for my students. It comes with the complete source code (on GitHub).

With regard to your code: you call LibraryWindowController a controller, but from the looks of it I assume it's an Application? You don't need to create separate applications to create multiple windows. Simply create a new stage with a scene and a root node. However, the steps you used should have worked, even if they aren't recommended. I'm guessing either login.getText() returns null or you have an error elsewhere in your code.



来源:https://stackoverflow.com/questions/30446878/how-to-pass-arguments-in-javafx

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