Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load fxml file inside Pane?

enter image description here

If we have a Stage then Scene includes 2 Panes the 1st Pane contains Button and the 2nd Pane is empty could we load other fxml file inside this 2nd Pane?

fxml1: VBox
               |_Pane1-->Button
               |_Pane2
///////////////
fxml2: Pane--> Welcome to fxml 2
"when we click the button load the fxml2 inside Pane2 of fxml1"

Then after click

enter image description here


====I finally found this works after trying !====Thank you guys

@FXML Pane secPane;
public void loadFxml (ActionEvent event) {
Pane newLoadedPane =        FXMLLoader.load(getClass().getResource("/application/fxml2.fxml"));
secPane.getChildren().add(newLoadedPane); 
}  
like image 377
Hussein Ahmed Avatar asked Nov 17 '15 02:11

Hussein Ahmed


People also ask

How do I load a FXML file from another package?

Parent root = FXMLLoader. load(new URL("/jfxtest2/Screen. fxml"));

Is FXML the view?

From a Model View Controller (MVC) perspective, the FXML file that contains the description of the user interface is the view. The controller is a Java class, optionally implementing the Initializable class, which is declared as the controller for the FXML file.


3 Answers

I finally found this works after trying !

@FXML Pane secPane;
public void loadFxml (ActionEvent event)  {
  Pane newLoadedPane =  FXMLLoader.load(getClass().getResource("/application/fxml2.fxml"));
  secPane.getChildren().add(newLoadedPane);
}
like image 181
Hussein Ahmed Avatar answered Oct 17 '22 04:10

Hussein Ahmed


Just replacing the field in your controller class won't change the scene graph.

secPane is just a reference to a node in the scene graph.

If secPane is just a placeholder, you could replace it in the parent's child list:

public void loadFxml (ActionEvent event) {
    // load new pane
    Pane newPane = FXMLLoader.load(getClass().getResource("/application/Login2.fxml"));

    // get children of parent of secPane (the VBox)
    List<Node> parentChildren = ((Pane)secPane.getParent()).getChildren();

    // replace the child that contained the old secPane
    parentChildren.set(parentChildren.indexOf(secPane), newPane);

    // store the new pane in the secPane field to allow replacing it the same way later
    secPane = newPane;
}

This assumes of course, that getClass().getResource("/application/Login2.fxml") yields the correct resource and does not return null (which happens if no resource with the given name is available)

like image 39
fabian Avatar answered Oct 17 '22 03:10

fabian


You can implement something like this :

 public void start(Stage primaryStage) throws IOException {
            primaryStage.setTitle("Title");
            primaryStage.setScene(createScene(loadMainPane("path_of_your_fxml")));
            primaryStage.show();

    }

    private Pane loadMainPane(String path) throws IOException {
        FXMLLoader loader = new FXMLLoader();

        Pane mainPane = (Pane) loader.load(
                getClass().getResourceAsStream(path));

        return mainPane;
    }


    private Scene createScene(Pane mainPane) {
        Scene scene = new Scene(mainPane);
      return scene;
    }

Then you can create a separate class call Navigation to store all your fxml paths:

public class Navigator {

private final String P1;
private final String P2;
//then you can implement getters...
public String getP1() {
    return P1;
}

public String getP2() {
    return p2;
}

private static FxmlController Controller;

    public static void loadPane(String fxml) {
    try {
        FxmlController.setPane(
                (Node) FXMLLoader.load(Navigator.class.getResource(fxml)));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public Navigator() throws IOException {
    this.P1 = "p1.fxml";
    this.P2 = "p2.fxml";}

Then you can load your pane in your button like below:

@FXML
    private void btnAction(ActionEvent event) throws IOException {
    Navigator.load(new Navigator().getP1());
    ..

.

like image 1
Madushan Perera Avatar answered Oct 17 '22 03:10

Madushan Perera