2014-09-19 83 views
1

我正在使用eclipse和场景生成器。javafx - 如何在运行时将fxml元素添加到选项卡面板中?

我想在运行时添加elemnts到选项卡。 每个元素是fxml文件(其中包含按钮,tableView,文本框...)。

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

我需要你的帮助,因为我无法找到一种方法如何做到这一点? 如何添加任何元素到标签? 如何将描述元素(fxml)添加到选项卡中?

感谢

+0

能否请您解释一下“添加上运行时元素标签每一个元素都是FXML文件。”据我所知,你可以添加一个[内容](http://docs.oracle.com/javafx/2/api/javafx/scene/control/Tab.html#setContent%28javafx.scene.Node%29)在标签中。你能解释你在这里想达到什么吗? – ItachiUchiha 2014-09-19 08:26:18

回答

1

我不知道我完全理解你的使用情况,但有一个看看FX:根结构:

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的中央:

<dependency> 
    <groupId>org.drombler.commons</groupId> 
    <artifactId>drombler-commons-fx-core</artifactId> 
    <version>0.4</version> 
</dependency> 
相关问题