1

I have three controllers, one that holds my Boderpane component CrechHome that switches between views, and another that has a button where my code resides EnfantController , and one where I need that button to direct me PaymentController I need that button to do something in the scene I'm in and then show another view (Payment), for that I'm loading my CrechHome controller so I can get the borderPane instance to switch to the other view, but the the following does nothing (I'm still in the same view as before) and shows no error.

Here's the code:

public class EnfantController implements Initializable {
    @FXML
    private Button payement;

    @FXML
    private void payementEnf(ActionEvent event) throws IOException {
        // get the crechHomeController to get the borderPane
        FXMLLoader loader2 = new FXMLLoader();
        loader2.setLocation(getClass().getResource("/Views/CrechHome.fxml"));
        Node node2 = loader2.load();
        CrechHomeController crechController = loader2.getController();
        // get the paymentController to get the payment view
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("/Views/Payment.fxml"));
        Node node = loader.load();

        PaymentController paymentController = loader.getController();
        paymentController.reload();
        paymentController.getRecherche().getSelectionModel().selectLast();

        crechController.getBorderPane().setCenter(node);
    }
}

I realize I may not be getting the correct Controller instance for the CrechHomeController and taht's why I'm not getting anything, but how do you fix this if that's what is wrong, also I'm a Javafx newbie, so all help will be appreciated.

Gherbi Hicham
  • 2,416
  • 4
  • 26
  • 41
  • You can try making the CrechHomeController a singleton class, then there will be only one instance – IR Emon Oct 03 '17 at 10:59
  • @IREmon How do you do that in JavaFX? Since controllers in Javafx do not need any constructors to get instantiated, the fact that I need to load the view in order to get the controller is just weird, how can I create a usefull singleton class for a controller in javaFx with that in mind? – Gherbi Hicham Oct 03 '17 at 11:05
  • @GherbiHicham You really can't make controllers singletons, for a number of reasons. – James_D Oct 03 '17 at 12:10
  • Somewhere in your code, you load `CrechHome.fxml` *and display the resulting UI*. You need to get the controller instance from that `FXMLLoader`. You haven't posted the code that does that, and we don't know the relationship between that code and the code you have posted, so it's really quite difficult to give a complete answer. See if you can create a [MCVE]. Maybe https://stackoverflow.com/questions/14187963 helps, or try a search like [this](https://stackoverflow.com/search?q=%5Bjavafx%5D+access+parent+controller+from+child+controller) – James_D Oct 03 '17 at 12:16
  • @ James_D I'm loading in the start method since it's the first thing I see in teh application. – Gherbi Hicham Oct 03 '17 at 13:28

1 Answers1

0

You haven't really provided enough information to provide a sure answer to this, but if you are loading the FXML files that have CrecheController and EnfantController as their controllers, respectively, in the same place you can do something like this;

public class CrecheController {

    @FXML
    private BorderPane borderPane ;

    public ObjectProperty<Node> displayedViewProperty() {
        return borderPane.centerProperty();
    }

    // ...
}
public class EnfantController {

    private final ObjectProperty<Node> node = new SimpleObjectProperty<>();

    public ObjectProperty<Node> nodeProperty() {
        return node ;
    }

    @FXML
    private void payementEnf(ActionEvent event) throws IOException {

        // get the paymentController to get the payment view
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("/Views/Payment.fxml"));
        Node node = loader.load();

        PaymentController paymentController = loader.getController();
        paymentController.reload();
        paymentController.getRecherche().getSelectionModel().selectLast();

        this.node.set(node);
    }

    // ...
}

Then when you load both FXML files just do

FXMLLoader crecheLoader = new FXMLLoader(getClass().getResource("/Views/CrechHome.fxml"));
Parent crecheView = crecheLoader.load();
CrecheController crecheController = crecheLoader.getController();

FXMLLoader enfantLoader = new FXMLLoader(getClass().getResource("/Views/EnfantHome.fxml"));
Parent enfantView = enfantLoader.load();
EnfantController enfantController = enfantLoader().getController();

enfantLoader.nodeProperty().bindBidirectional(crecheController.displayedViewProperty());

Similar solutions to this will work if you cannot do this directly like this.

James_D
  • 201,275
  • 16
  • 291
  • 322