我试图让我可以从另一个标签中为我的TabPane创建一个新选项卡,但我遇到了一些困难。目前我在main-window.fxml中设置了TabPane,并带有相应的MainWindowController。我在这个TabPane中有一个标签,通过fx:include,在MainTabController控制的场景图中显示mainTab.fxml。现在从mainTab中我想要一个按钮,以便能够向TabPane添加一个额外的选项卡,但由于这需要在主窗口中引用TabPane,因此我在main- window中创建了一个静态方法窗口。当运行下面的代码时,我在MainWindowController中的这一行得到一个NullPointerException:
I am trying to make it such that I can create a new tab for my TabPane from within another tab but I am having some difficulty. Currently I have the TabPane set up in the "main-window.fxml" with the corresponding MainWindowController. I have a tab within this TabPane which, via fx:include, displays "mainTab.fxml" to the scene graph, controlled by MainTabController. Now from within the "mainTab" I want a button to be able to add an additional tab to the TabPane, but since this is requires a reference to the TabPane in "main-window", I have created a static method in "main-window". When the run the code below I get a NullPointerException on this line in the MainWindowController:
mainTabPane.getTabs().add(new Tab(team.getTeamName()));有人可以告诉我为什么它会给出这个例外以及我如何开始解决它?
Could someone please tell me as to why it is giving this exception and how I can begin to work around it?
main-window.fxml:
main-window.fxml:
<TabPane fx:id="mainTabPane"> <tabs> <Tab fx:id="mainTab" text="Main" closable="false"> <fx:include source="mainTab.fxml" fx:id="mainWindowTab" alignment="CENTER"/> </Tab> </tabs> </TabPane>mainTab.fxml(按钮的事件处理程序):
mainTab.fxml (the event handler for the button):
@FXML public void handleSubmit() { String teamName = teamNameTextField.getText(); Roster roster = rosterComboBox.getValue(); int startWeek = spinner.getValue(); Team newTeam = new Team(teamName, startWeek, roster); TeamData.addTeam(newTeam); MainWindowController controller = new MainWindowController(); controller.createTeamTab(newTeam); }MainWindowController:
MainWindowController:
public class MainWindowController { @FXML private TabPane mainTabPane; public void createTeamTab(Team team) { mainTabPane.getTabs().add(new Tab(team.getTeamName())); } }推荐答案
您的代码不起作用,因为您没有在控制器上调用 createTeamTab(...):您在另一个 MainWindowController 。 (注释 @FXML 的字段在加载FXML时由 FXMLLoader 在控制器实例中初始化:相当明显原因是它们不会在同一个类的任意其他实例中设置为相同的值。)您需要获取对主选项卡使用的控制器的引用,并将引用传递给主控制器。
Your code doesn't work because you are not calling createTeamTab(...) on the controller: you are calling it on another instance of MainWindowController that you created. (The fields annotated @FXML are initialized in the controller instance by the FXMLLoader when the FXML is loaded: for fairly obvious reasons they will not be set to the same values in arbitrary other instances of the same class.) You need to get a reference to the controller you are using for the main tab, and pass it a reference to the main controller.
您没有告诉我们 mainTab.fxml 的控制器的类名:我假设它是 MainTabController (所以只需将其更改为您实际使用的任何类名)。
You didn't tell us the class name for the controller of mainTab.fxml: I will assume it is MainTabController (so just change it to whatever class name you actually use).
在 MainWindowController中,do:
public class MainWindowController { @FXML private TabPane mainTabPane; @FXML // fx:id of the fx:include with "Controller" appended private MainTabController mainWindowTabController ; public void initialize() { mainWindowTabController.setMainWindowController(this); } public void createTeamTab(Team team) { mainTabPane.getTabs().add(new Tab(team.getTeamName())); } }然后在 MainTabController do
public class MainWindowController { private MainWindowController mainWindowController ; public void setMainWindowController(MainWindowController mainWindowController) { this.mainWindowController = mainWindowController ; } @FXML public void handleSubmit() { String teamName = teamNameTextField.getText(); Roster roster = rosterComboBox.getValue(); int startWeek = spinner.getValue(); Team newTeam = new Team(teamName, startWeek, roster); TeamData.addTeam(newTeam); mainWindowController.createTeamTab(newTeam); } }更多推荐
JavaFX:从选项卡控制器添加新选项卡
发布评论