如何在运行时将fxml元素添加到选项卡面板中



我正在使用eclipse和场景生成器。

我想把运行时的元素添加到选项卡中。每个元素都是fxml文件(包含按钮、表视图、文本框…)

选项卡将不包含固定数量的元素。

我需要你的帮助,因为我找不到办法怎么做?如何在选项卡中添加任何元素?如何将描述的元素(fxml)添加到选项卡中?

感谢

我不确定我是否完全理解您的用例,但请查看fx:root构造:

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

在同一个包中添加一个MyCustomtPane.fxml文件:

<?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>

在同一个包中添加一个MyCustomtPane.properties文件:

label.text=Some text

你可以在代码中的其他地方使用:

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

注意:FXMLLoaders是我编写的一个实用程序类。该库是开源的,可直接从Maven Central获得:

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

最新更新