I am not able to add new tab functionality to browser.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 Main.java file
package tabcheck;
import javafx.application.Application;
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;
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("http:
webEngine.locationProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
addressField.setText(newValue);
}
});
addressField.setText("http:
}
public String fload(ActionEvent ae )
{
}
@FXML
private void addTab() throws IOException{
Tab tab = new Tab("New Tab");
tabpane.getTabs().add(tab);
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("/tabcheck/Tab-First-page.fxml"));
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 new tabs. Following is the error I am getting...
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
**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 working,please any one can help?
What I have tried:
I have also try to do it by adding listener in addTab() but still null pointer exception is coming...also search on other websites but nothing is working...