如何在JavaFX中从TabPane添加/删除Tab?

时间:2015-06-26 06:39:39

标签: java javafx

如何在JavaFX中从TabPane添加/删除Tab?

我正在尝试在JavaFX的Pane中使用TabPane来添加和删除TabPane中的选项卡。我正在使用基于FXML的布局,如下所示:

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.VBox?>


<Pane id="pane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.Controller">
 <children>
  <TabPane id="tabPane" prefHeight="328.0" prefWidth="600.0" tabClosingPolicy="ALL_TABS">
    <tabs>
      <Tab text="Tab1">
        <content>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="320.0" prefWidth="600.0" />
        </content>
      </Tab>
      <Tab text="Tab2">
        <content>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
        </content>
      </Tab>
    </tabs>
  </TabPane>
  <Button id="tabButton" layoutX="274.0" layoutY="361.0" mnemonicParsing="false" onAction="#handleTabButton" text="Button" />
</children>
</Pane>

我有MainApp来处理我的应用程序和控制器来处理事件。我的MainApp如下:

package src;

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

public class MainApp extends Application{

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

@Override
public void start(Stage primaryStage) throws Exception {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("LMainApp.fxml"));
    Parent root = loader.load();

    Controller controller = loader.getController();
    controller.setStage(primaryStage);

    primaryStage.setTitle("TestTabs");

    primaryStage.setScene(new Scene(root));
    primaryStage.show();
}

}

我的控制器如下:

package controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.Stage;

public class Controller implements Initializable{

Stage stage;

@FXML
private TabPane tabPane;

private Tab tab = new Tab();
private SingleSelectionModel<Tab> selectionModel;

@Override
public void initialize(URL location, ResourceBundle resources) {
    selectionModel = tabPane.getSelectionModel();
}

public void setStage(Stage stage){
    this.stage = stage;
}

@FXML
private void handleTabButton(ActionEvent event){
    tab.setText("New Tab");
    tab.setId("newTab");
    tab.setClosable(true);
    tabPane.getTabs().add(tab);
    selectionModel.selectLast();
}

}

现在,当我启动应用程序时,我在selectionModel = tabPane.getSelectionModel()时得到NullPointerException;在我的控制器中。似乎tabPane为空。

堆栈跟踪:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda$50/1645995473.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException: 
/E:/EclipseWorkspace/TestTabJFX/bin/src/LMainApp.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at src.MainApp.start(MainApp.java:19)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$$Lambda$53/434272560.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$46/186276003.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/463228645.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
    ... 1 more
Caused by: java.lang.NullPointerException
    at controller.Controller.initialize(Controller.java:26)
    ... 18 more
Exception running application src.MainApp

有人可以建议如何解决这个问题吗?此外,如果有人可以帮助我 - 如何访问/使用FXML文件中的元素。 提前谢谢。

1 个答案:

答案 0 :(得分:3)

您在fxml中使用的是id而不是fx:id。将TabPane的声明更改为

<TabPane fx:id="tabPane" prefHeight="328.0" prefWidth="600.0" tabClosingPolicy="ALL_TABS">

了解更多信息: