繁体   English   中英

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

[英]How to add/remove Tab from TabPane in JavaFX?

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

我正在尝试在JavaFX的窗格内使用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来处理我的应用程序,还有一个Controller处理事件。 我的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文件中存在的元素。 提前致谢。

您正在fxml中使用id代替fx:id TabPane的声明更改为

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

欲获得更多信息 :

暂无
暂无

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

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