Reputation: 27
I'm trying to populate the TextField from a scene with the value selected in another scene.
So there's a scene (I'll call it parent scene) with a TextField and a button which opens another scene (I'll call it child scene). The child scene has a TableView which selected value I want to set to the parent scene's TextField. I know how to do this only if the scene with the TextField is not already opened, in my case the child scene's stage being setted as showAndWait. So I think I need to get the parent scene's stage and set it to the stage field in select() method below from the child controller.
// method from child controller, called when selecting a row from the tableview
public void select() throws IOException
{
Class class = (Class)table.getSelectionModel().getSelectedItem();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getClassLoader().getResource("parentStage.fxml"));
Parent parent = loader.load();
Stage stage = // here i need to pass the parent stage I think;
stage.initModality(Modality.APPLICATION_MODAL);
ParentController parentController = loader.getController();
table.setOnMouseClicked(event -> {
if(event.getClickCount()==2){
parentController.setTextField(new TextField(class.getValue()));
stage.setScene(new Scene(parent));// also,
I think, here it should be passed the already opened parent scene
stage.show();
((Node)(event.getSource())).getScene().getWindow().hide();
} });
// this is the method for the button from parent scene that opens the child scene
public void add() throws IOException
{
Parent parent = FXMLLoader.load(getClass().getClassLoader().getResource("childStage.fxml"));
stage = new Stage();
stage.initModality(Modality.APPLICATION_MODAL);
stageProdusNou.setScene(new Scene(parent));
stageProdusNou.showAndWait();
}
Thanks.
Upvotes: 0
Views: 502
Reputation:
You can do this without any need to reload the base layout, by:
This answer is a summarization of the codes and steps here.
Upvotes: 0