0

I am making a browser using javafx but I am not able to add new tab functionality in it.I already search a lot on google as well as on stackoverflow but nothing work.I want to load main fxml file again by clicking on new tab button.

Below is home page

First-page.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.*?>
<?import javafx.scene.input.*?>
<!-- all imports -->

<AnchorPane xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tabcheck.Home_controller">
   <children>
      <TabPane fx:id="tabpane" prefHeight="629.0" prefWidth="1024.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <tabs>
            <Tab fx:id="tab1" text="Tab1">
               <content>
                  <AnchorPane>
                     <children>
                        <BorderPane fx:id="mainLayout" prefHeight="600.0" prefWidth="1024.0">
                           <top>
                              <HBox fx:id="Hbox" prefHeight="54.0" prefWidth="1024.0" spacing="15.0" BorderPane.alignment="CENTER">
                                 <children>
                                  <Button fx:id="go_btn" mnemonicParsing="false" onAction="#G" prefHeight="25.0" prefWidth="30.0"/>
                                  <Button fx:id="smartnote_btn" layoutX="419.0" layoutY="10.0" mnemonicParsing="false" onAction="#OpenNotes" prefHeight="25.0" prefWidth="30.0"/>
                                  <Button fx:id="newtab_btn" layoutX="523.0" layoutY="10.0" mnemonicParsing="false" onAction="#addTab" prefHeight="25.0" prefWidth="30.0"/>
                                 <MenuButton fx:id="setting_btn" mnemonicParsing="false" prefHeight="25.0" prefWidth="50.0">
                                   <items>
                                   <MenuItem mnemonicParsing="false" text="Bookmark" />
                                   </items>

                                                                                                                       </MenuButton>
                                 </children>
                                </HBox>
                           </top>
                           <center>
                              <WebView fx:id="webView" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />

                                                                    </center>
                        </BorderPane>
                     </children>
                  </AnchorPane>
               </content>
            </Tab>
         </tabs>
      </TabPane>
   </children>
</AnchorPane>

Below is Main.java file

package tabcheck;
import javafx.application.Application;
 //all imports..
public class Main extends Application{

public static void main(String[] args){
  launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
try{
      Parent root = FXMLLoader.load(getClass().getResource("/tabcheck/First    page.fxml"));
      Scene scene = new Scene(root);
      scene.getStylesheets().add("tabcheck/application.css");
      primaryStage.setScene(scene);
     primaryStage.show();
  } 
   catch(Exception e){ e.printStackTrace();}
  }
}

Below is controller of First-page.fxml

Home_controller.java

 package tabcheck;
 import java.io.IOException;
 import java.net.URL;
 //All imports...

 public class Home_controller implements Initializable{
    @FXML private Button forward_btn;
    @FXML private Button refresh_btn;
    @FXML private Button go_btn;
    @FXML private TextField addressField;
    @FXML private WebView webView;
    @FXML private TabPane tabpane ;
    @FXML private BorderPane mainLayout;
     private Main main;
     private WebEngine webEngine;

 @Override
  public void initialize(URL url, ResourceBundle rb){
      webEngine = webView.getEngine();
      webEngine.load(&amp;quot;http://www.google.com&amp;quot;);
      webEngine.locationProperty().addListener(new ChangeListener&amp;lt;String&amp;gt;()        {
      @Override
      public void changed(ObservableValue&amp;lt;? extends String&amp;gt; observable, String oldValue, String newValue) {
          addressField.setText(newValue);
      }
     });
      addressField.setText(&amp;quot;http://www.google.com&amp;quot;);
    }

public String fload(ActionEvent ae )
{// code....
 }
 @FXML
 private void addTab() throws IOException{
     Tab tab = new Tab(&amp;quot;New Tab&amp;quot;);
     tabpane.getTabs().add(tab);
    // tabpane.getSelectionModel().clearSelection();
     FXMLLoader loader = new FXMLLoader();
     loader.setLocation(Main.class.getResource(&amp;quot;/tabcheck/Tab-First-page.fxml&amp;quot;));
     AnchorPane tabAnchor = loader.load();
     tab.setContent(tabAnchor);
  }

In Controller class Tab-First-page.fxml is the copy of first-page.fxml and changes in Tab-first-page.fxml is that it does not contain Tabpane otherwise it is same.

I am able to load multiple tab from First-page.fxml but not able to open new tab from Tab-first-page.fxml that is, From main page multiple tabs can be open dynamically but from those open tabs I am not able to open tabs. Following is the error I am getting...

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
    //whole stack of error...
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
    ... 53 more
**Caused by: java.lang.NullPointerException
    at tabcheck.Home_controller.addTab(Home_controller.java:200)
    ... 62 more**

Home_controller.java:200 is tabpane.getTabs().add(tab); in addTab() method. I have tried every possible solution but not able to load main page content again from sub open tab,please any one can help?

2
  • 1
    Welcome to StackOverflow! You used the wrong formatting for your code blocks. Code snippets are used to run certain kinds of code, not yours. Use a code block instead. Also remove all parts of the code that are not strictly necessary for reproducing the problem. We only want minimal examples, otherwise chances are great nobody even will look at your code. Mar 17, 2016 at 10:39
  • Sry about this as I am new to stackoverflow don't know about this ...even though i edited it and try to put as much information as possible....please any body can help me out as I have been trying from days but not able to solve the error....
    – user123
    Mar 17, 2016 at 12:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.