当前位置: 动力学知识库 > 问答 > 编程问答 >

java - javafx - init tabs (of tab pane) on startup

问题描述:

I'm using eclipse & scene builder.

I has defined the main window as follow:

I want to define the "Outgoing Messages" tab with X (X is calculated on run time) of elements (the elements defined at different fxml file).

When I'm creating the main window:

private Stage primaryStage;

private AnchorPane rootLayout;

public void start(Stage primaryStage) {

this.primaryStage = primaryStage;

this.primaryStage.setTitle("ABC");

try {

// Load root layout from fxml file.

FXMLLoader loader = new FXMLLoader();

loader.setLocation(MainApp.class.getResource("MainLayout.fxml"));

rootLayout = (AnchorPane) loader.load();

// Show the scene containing the root layout.

Scene scene = new Scene(rootLayout);

primaryStage.setScene(scene);

primaryStage.show();

} catch (IOException e) {

e.printStackTrace();

}

}

How can I update the "Outgoing Messages" tabs as I described above ?

Thanks

网友答案:

You may use the <fx:include> inside your FXML for Outgoing Messages. This will automatically load X number of fxml before loading the main FXML. After all the incude FXML's are loaded, the FXML for Outgoing Messages is prepared and shown.

Update

If you have dynamic increase/decrease in elemtents, use the initialize() of the Outgoing Messages to populate the elements inside it instead of using <fx:incude>.

Lets consider (you may have your own logic) your Outgoing Messages has a VBox which is to be filled with dynamic no of elements(BorderPane in this case) which is inside X.fxml (You may have different FXML or you can create you own control at runtime and add it) :

pubic class OutgoingMessagesController implements Initializable {

    @FXML
    private VBox vbox;

    public void initialize(java.net.URL location, java.util.ResourceBundle resources) {

        //Whatever your logic is, I am considering a dynamic number here

        for(int i=0; i<dynamicNumber; i++){
            BorderPane borderPane = FXMLoader.load(getClass().getResource("X.fxml"));
            vbox.getChildren.add(borderPane);
        }
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: