简体   繁体   中英

javafx - how to add fxml elements on run time into tab panel?

I'm using eclipse and scene builder.

I want to add elemnts on run time to tabs. Each element is fxml file (which contains button, tableView, textbox...).

The Tab will not contain fix number of elements.

I need yours help, because I cant find a way how to do it ? How can I add any eleemnt to tab ? How can I add the decribed elements (fxml) into the tab ?

Thanks

I'm not sure I'm understanding your use case completely, but have a look at the fx:root construct:

import java.io.IOException;
import javafx.scene.layout.BorderPane;

import org.drombler.commons.fx.fxml.FXMLLoaders;

public class MyCustomtPane extends BorderPane {
    public TopTestPane() throws IOException {
        loadFXML();
    }

    private void loadFXML() throws IOException {
        FXMLLoaders.loadRoot(this);
    }
} 

In the same package add a MyCustomtPane.fxml file:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<fx:root type="BorderPane" xmlns:fx="http://javafx.com/fxml">
    <center>
        <Label text="%label.text"/>
    </center>
</fx:root>

In the same package add a MyCustomtPane.properties file:

label.text=Some text

Somewhere else in your code you can use:

...
MyCustomtPane myCustomtPane = new MyCustomtPane(); // will load the FXML as well
addToProperPlace(myCustomtPane);

Note: FXMLLoaders is a utility class I've written. The library is Open Source and available directly from Maven Central:

<dependency>
    <groupId>org.drombler.commons</groupId>
    <artifactId>drombler-commons-fx-core</artifactId>
    <version>0.4</version>
</dependency>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM