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

时间:2014-09-19 06:02:07

标签: javafx

我正在使用eclipse和场景构建器。

我想在运行时添加elemnts到标签页。 每个元素都是fxml文件(包含button,tableView,textbox ......)。

标签不包含元素的固定数量。

我需要你的帮助,因为我找不到怎样的方法呢? 如何将任何eleemnt添加到标签? 如何将所描述的元素(fxml)添加到选项卡中?

由于

1 个答案:

答案 0 :(得分:1)

我不确定我是否完全了解您的用例,但请查看 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>