I'm trying to create a browser. I have an add button and when it's clicked it should create a new tab. The button works when it is clicked on the first initial tab when the program just opens. The newly created tab takes the whole contents of the initial tab so the same button is created in the new tab. The problem is I can't create a new tab when I click on the button that is on the newly created tab, only the button on the first tab works.
@FXML public void addNewTab(ActionEvent event) {
Tab newTab = new Tab("Untitled " + (tabPane.getTabs().size() + 1));
try {
tabPane.getTabs().add(newTab);
tabPane.getSelectionModel().select(newTab);
AnchorPane anchorPane = FXMLLoader.load(getClass().getResource("new-tab.fxml"));
newTab.setContent(anchorPane);
} catch(Exception e) {
e.printStackTrace();
System.out.println(e);
}
}
This is the FXML file of the program and initial first tab:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.web.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mybrowser.MyBrowserFXMLController">
<children>
<TabPane fx:id="tabPane" layoutY="28.0" prefHeight="691.0" prefWidth="1200.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab fx:id="tab1" text="Untitled">
<content>
<AnchorPane prefHeight="200.0" prefWidth="200.0">
<children>
<Pane prefHeight="50.0" prefWidth="1200.0" style="-fx-border-width: 0px 0px 1px 0px; -fx-border-color: #FEDC01;">
<children>
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="10.0" layoutY="12.0" mnemonicParsing="false" onAction="#addNewTab" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
<graphic>
<ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/add.png" />
</image>
</ImageView>
</graphic>
<padding>
<Insets left="-10.0" right="-10.0" />
</padding></Button>
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="40.0" layoutY="12.0" mnemonicParsing="false" onAction="#pagePrevious" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
<graphic>
<ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/back.png" />
</image>
</ImageView>
</graphic>
<padding>
<Insets left="-12.0" right="-10.0" />
</padding></Button>
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="70.0" layoutY="12.0" mnemonicParsing="false" onAction="#pageNext" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
<padding>
<Insets left="-10.0" right="-12.0" />
</padding>
<graphic>
<ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/forward.png" />
</image>
</ImageView>
</graphic></Button>
<TextField fx:id="addressBar" layoutX="110.0" layoutY="13.0" onKeyPressed="#enterURL" prefHeight="27.0" prefWidth="1050.0" promptText="Search or enter the website name" />
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="1163.0" layoutY="13.0" mnemonicParsing="false" onAction="#pageRefresh" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
<graphic>
<ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/refresh.png" />
</image>
</ImageView>
</graphic>
<padding>
<Insets left="-12.0" right="-10.0" />
</padding>
</Button>
</children>
</Pane>
<WebView fx:id="webView" layoutY="50.0" prefHeight="609.0" prefWidth="1200.0" />
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
<MenuBar prefHeight="29.0" prefWidth="1200.0">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="New Tab" />
<MenuItem mnemonicParsing="false" text="Close Tab" />
<MenuItem mnemonicParsing="false" text="Close All Tabs" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="New Window" />
<MenuItem mnemonicParsing="false" text="Close Window" />
<MenuItem mnemonicParsing="false" text="Close All Windows" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Close Browser" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Select All" />
<MenuItem mnemonicParsing="false" text="Copy" />
<MenuItem mnemonicParsing="false" text="Paste" />
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="View">
<items>
<MenuItem mnemonicParsing="false" text="Zoom In" />
<MenuItem mnemonicParsing="false" text="Zoom Out" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="History">
<items>
<MenuItem mnemonicParsing="false" onAction="#historyFull" text="Show Full History" />
<Menu mnemonicParsing="false" text="Show Recent History...">
<items>
<MenuItem mnemonicParsing="false" />
</items>
</Menu>
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Delete History..." />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="Find..." />
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
</children>
</AnchorPane>
This is the FXML for the new tab ("new-tab.fxml"):
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.web.*?>
<AnchorPane prefHeight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mybrowser.MyBrowserFXMLController" >
<children>
<Pane prefHeight="50.0" prefWidth="1200.0" style="-fx-border-width: 0px 0px 1px 0px; -fx-border-color: #FEDC01;">
<children>
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="10.0" layoutY="12.0" mnemonicParsing="false" onAction="#addNewTab" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
<graphic>
<ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/add.png" />
</image>
</ImageView>
</graphic>
<padding>
<Insets left="-10.0" right="-10.0" />
</padding>
</Button>
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="40.0" layoutY="12.0" mnemonicParsing="false" onAction="#pagePrevious" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
<graphic>
<ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/back.png" />
</image>
</ImageView>
</graphic>
<padding>
<Insets left="-12.0" right="-10.0" />
</padding>
</Button>
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="70.0" layoutY="12.0" mnemonicParsing="false" onAction="#pageNext" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
<graphic>
<ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/forward.png" />
</image>
</ImageView>
</graphic>
<padding>
<Insets left="-10.0" right="-12.0" />
</padding>
</Button>
<TextField fx:id="addressBar" layoutX="110.0" layoutY="13.0" onKeyPressed="#enterURL" prefHeight="27.0" prefWidth="1050.0" promptText="Search or enter the website name" />
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="1163.0" layoutY="13.0" mnemonicParsing="false" onAction="#pageRefresh" prefHeight="27.0" prefWidth="27.0" styleClass="buttons">
<graphic>
<ImageView fitHeight="20.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../images/refresh.png" />
</image>
</ImageView>
</graphic>
<padding>
<Insets left="-12.0" right="-10.0" />
</padding>
</Button>
</children>
</Pane>
<WebView fx:id="webView" layoutY="50.0" prefHeight="609.0" prefWidth="1200.0" />
</children>
</AnchorPane>
As you can see, it is only the contents of the first tab. This FXML file has the same controller as the last FXML file which includes the "base" of the whole GUI. The buttons in this FXML have also been assigned to the correct ActionEvent in the controller. But I keep on getting this error with the problem mentioned above:
Executing/Users/Reno/Desktop/MyBrowser/dist/run1010542926/MyBrowser.jar using platform /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home/jre/bin/java
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 49 more
Caused by: java.lang.NullPointerException
at mybrowser.MyBrowserFXMLController.addNewTab(MyBrowserFXMLController.java:55)
... 59 more