JavaFX TabPane - 每个选项卡的一个控制器 [英] JavaFX TabPane - One controller for each tab

查看:8252
本文介绍了JavaFX TabPane - 每个选项卡的一个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Fx的新手。我有一个TabPanel与10选项卡。每个Tab都有许多控件(图表,按钮等),我想要的是为每个Tab分配一个控制器。 SceneBuilder只允许我为整个视图分配一个控制器,我的意思是,只有顶部面板(根)有Controller类选项,所以我必须为一个类中的所有选项卡编写代码,需要,导致一个非常大的类,很难理解和维护。
也许解决方案很简单,但正如我所说,我对FX的经验很少,我还没有能够在网上找到类似的东西。



任何想法?谢谢。

解决方案

一种方法是将每个标签页封装到一个单独的FXML文件中,



然后在主选项卡控件的FXML文件中,您可以执行以下操作:

 < TabPane> 
< tabs>
< Tab text =Untitled Tab 1>
< content>
< fx:include fx:id =fooTabPagesource =fooTabPage.fxml/>
< / content>
< / Tab>
< Tab text =Untitled Tab 2>
< content>
< fx:include fx:id =barTabPagesource =barTabPage.fxml/>
< / content>
< / Tab>
< / tabs>
< / TabPane>

请注意,我不是直接嵌入内容,而是使用 fx: include 指令,它告诉 FXMLLoader 加载被引用的FXML文件。



如果您需要与子页面或子页面进行交互,那么用于页面内容的单个FXML文件将具有自己的控制器,

  public class MainController {
//注入标签内容。
@FXML private FooTabPage fooTabPage;
//注入控制器
@FXML private FooTabController fooTabPageController;

//注入标签内容。
@FXML private BarTabPage barTabPage;
//注入控制器
@FXML private BarTabController barTabPageController;
}

如果您有大量的页面自己的控件)另一种方法是将每个选项卡留空,并且一旦加载了主视图,将相关页面加载到控件中。



您需要监听标签页更改以切换内容并添加相关代码来加载/卸载用于标签页内容的视图。 / p>

我建议从第一种方法开始,如果发现性能问题,重构使用第二种方法。


I'm new to Fx. I have a TabPanel with 10 Tabs. Each Tab has many controls (charts, buttons, etc.), and what I want is to assign a controller for each Tab. The SceneBuilder only let me assign a controller for the whole view, I mean, only the top panel (the root) has the "Controller class" option, so I have to write the code for all the tabs in one class and this, as entail, resulting in a very large class and difficult to understand and maintain. Perhaps the solution is very simple, but as I say, I have very little experience with FX and I have not been able to find something similar on the web.

Any idea? Thank you.

解决方案

One approach is to encapsulate each of your tab pages into a separate FXML file with it's own associated controller class.

Then in your FXML file for the main tab control you can do something like this:

<TabPane >
    <tabs>
        <Tab text="Untitled Tab 1">
            <content>
                <fx:include fx:id="fooTabPage" source="fooTabPage.fxml"/>
            </content>
        </Tab>
        <Tab text="Untitled Tab 2">
            <content>
                <fx:include fx:id="barTabPage" source="barTabPage.fxml"/>
            </content>
        </Tab>
    </tabs>
</TabPane>

Notice that instead of embedding the content directly, I am using the fx:include directive which tells the FXMLLoader to load the FXML file that is being referenced. The individual FXML files used for the page content will all have their own controller so that the logic is nicely separated.

If you need to interact with the sub-pages or sub-controllers from the main controller then you can reference them like you do with any other FXML control to have them injected.

public class MainController {
    // Inject tab content.
    @FXML private FooTabPage fooTabPage;
    // Inject controller
    @FXML private FooTabController fooTabPageController;

    // Inject tab content.
    @FXML private BarTabPage barTabPage;
    // Inject controller
    @FXML private BarTabController barTabPageController;
}

If you have a large number of pages (each with a large number of their own controls) another approach is to leave each tab empty, and once the main view is loaded, load the relevant page into your control.

You would need to listen for tab changes to switch the content and add relevant code to load / unload the views that are being used for the content of the tab pages.

I would recommend starting with the first approach and refactoring to use the second approach if you discover performance problems.

这篇关于JavaFX TabPane - 每个选项卡的一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆