JavaFX 可重用 FXML 片段

标签 java javafx fxml

我正在实现一个选项卡式部分,其中每个选项卡将包含一个 TableView 。在此 TableView 中,无论选择哪个选项卡,都会呈现一个列的子集,但某些选项卡将包含其他列(以编程方式处理)。

由于这些原因,每个选项卡都需要有一个单独的 Controller ,但我想知道是否可以在每个选项卡中重用选项卡 FXML 的内部内容,而无需复制和粘贴代码。我正在考虑一个可重用组件,我可以在另一个 FXML 文件中定义该组件,然后将其包含在选项卡部分中。

具体来说,我希望将下面代码中的内容部分放在一个文件中,并在每个文件中引用它,这样如果我将来想更改某些内容,就不必对多个文件进行更改。我认为这可以通过编程来实现,但如果可能的话,我更喜欢 FXML 解决方案。

<Tab xmlns:fx="http://javafx.com/fxml/1"
     xmlns="http://javafx.com/javafx/8"
     fx:controller="com.SpecificController"
     id="specificTab" text="Specific Tab Name">

    <content>
        <JFXTreeTableView fx:id="commonTableView" VBox.vgrow="ALWAYS">
            <columns>
                <JFXTreeTableColumn fx:id="nameColumn" text="Name"/>
                <JFXTreeTableColumn fx:id="positionColumn" text="Position"/>
                <JFXTreeTableColumn fx:id="teamColumn" text="Team"/>
                <JFXTreeTableColumn fx:id="selectColumn" text="Select"/>
            </columns>
        </JFXTreeTableView>
    </content>
</Tab>

最佳答案

我绝不是 JavaFX 专家,但我能够想出这个解决方案。

这是一个 MCVE,因此您可以完全重新创建此示例应用程序并运行它以查看其实际情况。

此示例使用 AnchorPane 作为可重用 FXML 节点,但您可以将其修改为使用 Tab。不过,我发现重用内容本身要简单一些。

我使用一个简单的Interface来定义我们的选项卡 Controller ;这允许我们编写一种方法来设置每个 Tab 的内容和单独的 Controller 。

<小时/>

Main.java:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        try {
            // Load the main layout
            FXMLLoader loader = new FXMLLoader(getClass().getResource("MainLayout.fxml"));

            // Show the Stage
            primaryStage.setWidth(700);
            primaryStage.setHeight(500);
            primaryStage.setScene(new Scene(loader.load()));
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

FXML Documents:

MainLayout.fxml:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1"
            xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.reusableTabs.MainController">
    <VBox alignment="TOP_CENTER" prefHeight="400.0" prefWidth="600.0" spacing="10.0" AnchorPane.bottomAnchor="0.0"
          AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <padding>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
        </padding>
        <Label style="-fx-font-size: 150%; -fx-font-weight: bold;" text="Reusable Tab Content Sample"/>
        <Separator prefWidth="200.0"/>
        <TabPane fx:id="tabPane" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE"
                 VBox.vgrow="ALWAYS">
            <Tab fx:id="tab1" text="Tab #1"/>
            <Tab fx:id="tab2" text="Tab #2"/>
            <Tab fx:id="tab3" text="Tab #3"/>
        </TabPane>
    </VBox>
</AnchorPane>

TabContent.fxml:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
            prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1">
    <VBox alignment="CENTER" prefHeight="400.0" prefWidth="600.0" spacing="20.0" AnchorPane.bottomAnchor="0.0"
          AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <Label text="Look - reusable Tab contents!"/>
        <Button fx:id="btnTestController" mnemonicParsing="false" text="Test Controller"/>
        <padding>
            <Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
        </padding>
    </VBox>
</AnchorPane>

Controllers:

MainController.java:

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;

import java.io.IOException;

public class MainController {

    @FXML
    private TabPane tabPane;
    @FXML
    private Tab tab1;
    @FXML
    private Tab tab2;
    @FXML
    private Tab tab3;

    @FXML
    private void initialize() {

        // Our TabPane has 3 Tabs. Let's populate them with content, reusing our TabContent.fxml file
        setTabContent(tab1, new Tab1Controller());
        setTabContent(tab2, new Tab2Controller());
        setTabContent(tab3, new Tab3Controller());

    }

    /**
     * Sets the content of Tab to the TabContent.fxml document
     *
     * @param tab        The Tab whose content we wish to set
     * @param controller The controller for this particular Tab's content
     */
    private void setTabContent(Tab tab, TabController controller) {

        // Load our TabContent.fxml file and set it as the content of the Tab that was passed to this method
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("TabContent.fxml"));

            // Set the controller for this specific tab
            loader.setController(controller);

            // Set the content of the passed Tab to this FXML content
            tab.setContent(loader.load());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Tab1Controller:

import javafx.fxml.FXML;
import javafx.scene.control.Button;

public class Tab1Controller implements TabController {

    @FXML
    private Button btnTestController;

    @FXML
    private void initialize() {

        // Set our button to print out which controller is being used
        btnTestController.setOnAction(event -> System.out.println("Hello, from Controller #1!"));

    }
}

只需为 Tab2Controller.java 和 `Tab3Controller.java 重复此 Controller 两次

<小时/>

The Result:

screenshot

单击测试 Controller 按钮将打印出Hello, from Controller #2!,确认每个选项卡的内容均由其自己的 Controller 控制。

关于JavaFX 可重用 FXML 片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53453630/

相关文章:

java - 仅更新表单中的单个字段,而 Struts 2 中的其他信息保持不变

java - kotlin 连接到自签名 https 服务器

css - Javafx禁用Node继承DropShadow的颜色

JavaFX 无法加载 FXML,找不到资源

java - 将 2 个变量从一个 Controller 传递到另一个 Controller

java - 将第二个 Activity 中图像的可见性设置为从我的主要 Activity 中可见?

java - 当后台服务执行密集型任务时,如何防止 GUI 延迟?

从命令行运行应用程序时 JavaFX 8 视频播放卡住

java - 如何使用 fxml JavaFX 运行 recorder.java?

java - Netbeans 模块开发内存不足