简体   繁体   English

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

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

I'm using eclipse and scene builder. 我正在使用Eclipse和Scene Builder。

I want to add elemnts on run time to tabs. 我想在运行时向选项卡添加元素。 Each element is fxml file (which contains button, tableView, textbox...). 每个元素都是fxml文件(包含按钮,tableView,文本框...)。

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 ? 如何将描述的元素(fxml)添加到选项卡中?

Thanks 谢谢

I'm not sure I'm understanding your use case completely, but have a look at the fx:root construct: 我不确定我是否完全理解您的用例,但请看一下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);
    }
} 

In the same package add a MyCustomtPane.fxml file: 在同一包中,添加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>

In the same package add a MyCustomtPane.properties file: 在同一程序包中,添加MyCustomtPane.properties文件:

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. 注意: FXMLLoaders是我编写的实用程序类。 The library is Open Source and available directly from Maven Central: 该库是开源的,可以直接从Maven Central获得:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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