Reputation: 1215
How to add/remove Tab from TabPane in JavaFX?
I am trying to use TabPane inside a Pane in JavaFX to add and remove tabs from TabPane. I am using FXML based layout which is as follows:
<?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>
I have MainApp to lauch my application and a Controller to handle events. My MainApp is as follows:
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();
}
}
and my controller is as follows:
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();
}
}
Now, when I launch the application, I get NullPointerException at selectionModel = tabPane.getSelectionModel(); in my controller. It seems tabPane is null.
StackTrace:
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
Could anyone please suggest how to solve the issue? Moreover if someone could help me in - how to access/use elements present in FXML file. Thanks in advance.
Upvotes: 0
Views: 4508
Reputation: 36722
You are using id
instead of fx:id
in your fxml. Change the declaration of TabPane
to
<TabPane fx:id="tabPane" prefHeight="328.0" prefWidth="600.0" tabClosingPolicy="ALL_TABS">
For more information :
Upvotes: 4