0
如何在JavaFX中從TabPane添加/刪除Tab?如何在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來啓動我的應用程序和控制器來處理事件。我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();
}
}
現在,當我啓動應用程序,我得到NullPointerException異常selectionModel的處= tabPane.getSelectionModel();在我的控制器中。看來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文件中的元素。 在此先感謝。
可以添加異常的堆棧跟蹤中的問題? – ItachiUchiha
Hi @ItachiUchiha,我已經添加了堆棧跟蹤。 –