2015-11-17 7 views
7

enter image description hereफलक के अंदर fxml फ़ाइल कैसे लोड करें?

अगर हम एक Stage तो Scene शामिल 2 Pane रों Button होता है और 2 Pane खाली है हम इस 2 Pane अंदर अन्य fxml फ़ाइल को लोड कर सकता है?

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 पेन सेकपेन काम नहीं करता; सार्वजनिक शून्य लोड Fxml (ActionEvent ईवेंट) { secPane = FXMLLoader.load (getClass()। GetResource ("/ application/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 वापस नहीं करता है (जो कोई संसाधन अगर ऐसा होता दिए गए नाम के साथ उपलब्ध है)

संबंधित मुद्दे