Подтвердить что ты не робот

Как загрузить файл fxml внутри панели?

enter image description here

Если у нас есть Stage, то Scene включает в себя 2 Pane с 1 - й Pane содержит Button и второй Pane пуста может загружаем другой файл FXML внутри этой 2 - й Pane?

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

Затем после нажатия

enter image description here


==== Я, наконец, нашел, что это работает после попытки! ==== Спасибо, ребята

@FXML Pane secPane;
public void loadFxml (ActionEvent event) {
Pane newLoadedPane =        FXMLLoader.load(getClass().getResource("/application/fxml2.fxml"));
secPane.getChildren().add(newLoadedPane); 
}  
4b9b3361

Ответ 1

Наконец я нашел это после попытки!

@FXML Pane secPane;
public void loadFxml (ActionEvent event)  {
  Pane newLoadedPane =  FXMLLoader.load(getClass().getResource("/application/fxml2.fxml"));
  secPane.getChildren().add(newLoadedPane);
}

Ответ 2

Просто замена поля в классе контроллера не изменит график сцены.

secPane - это просто ссылка на узел в графе сцены.

Если secPane является просто заполнителем, вы можете заменить его в родительском списке дочерних secPane:

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;
}

Это предполагает, конечно, что getClass().getResource("/application/Login2.fxml") дает правильный ресурс и не возвращает null (что происходит, если ресурс с заданным именем недоступен)

Ответ 3

Вы можете реализовать что-то вроде этого:

 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;
    }

Затем вы можете создать отдельный вызов класса Navigation для хранения всех ваших путей fxml:

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";}

Затем вы можете загрузить свою панель в свою кнопку, как показано ниже:

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

,