2015-11-17 32 views
7

enter image description here枠内にfxmlファイルを読み込む方法は?

我々はStageを持っているならば、私たちは、この第二Pane内の他のFXMLファイルを読み込むことができSceneは2 Pane Sを含ん 第一PaneButtonが含まれており、第二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); 
} 
+0

実際にfxmlファイルを動的に読み込むことができます。あなたの質問がこれ以上関与していない場合は、実際のコードとそれがうまくいかなかったものについてもう少し追加するために、それを編集する必要があります。 –

+0

これで試しても動作しません @FXMLペインsecPane; public void loadFxml(ActionEvent event){ secPane = FXMLLoader.load(getClass()。getResource( "/アプリケーション/ fxml2.fxml")); } –

+0

を実行しようとすると、どのようなエラーが表示されますか? –

答えて

6

私は最終的に試した後にこの作品を見つけました!

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

をありがとう次のようなものを実装してください:

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

次に、あなたはすべてのあなたの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()); 
    .. 

3

コントローラクラスのフィールドを置き換えても、シーングラフは変更されません。

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を(返さないことを、当然の前提としてい

関連する問題