Search code examples
javafxjavafx-8javafx-webengine

WebEngine not loading URL on button click


I am coding a Tabbed web browser in JAVAFx. The problem i am facing is :-
When I click on Home Button (HomeB) it is not loading the DEFAULT_URL in the current tab. Here is some useful part of my code. Some body please fix it. Thanks

        class Browser extends Region{  

            final BorderPane borderPane;  
            final TabPane    tabPane;
    private final HBox       toolbarMain;
                  WebView    browser    = new WebView();
            final  WebEngine webEngine  = browser.getEngine();
    private        String    DEFAULT_URL= "http://www.google.com";     
            final  TextField urlField   = new TextField(DEFAULT_URL);

    //Custom function for creation of New Tabs.  
    private Tab createAndSelectNewTab(final TabPane tabPane, final String title) {  
           Tab     tab         = new Tab(title);

            webEngine.locationProperty().addListener(new ChangeListener<String>() {  
                @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {  
                    urlField.setText(newValue); 
                }  
            }); 
           final ObservableList<Tab> tabs = tabPane.getTabs();  
           tab.closableProperty().bind(Bindings.size(tabs).greaterThan(2));  
           tabs.add(tabs.size() - 1, tab);  
           tabPane.getSelectionModel().select(tab);  
           return tab;  
    } 

    //Initialization function of the program.  
      public Browser() { 
                    borderPane        = new BorderPane();
                    tabPane           = new TabPane();
                    toolbarMain       = new HBox();
                    Button     HomeB  = new Button();
                    HomeB.setText("HOME");

           tabPane.setSide(Side.TOP);  

           final Tab newtab = new Tab();
           newtab.setText("+");


           newtab.setClosable(false); // this will not let the New Tab button(TAB) close  
           tabPane.getTabs().addAll(newtab); //Addition of New Tab to the tabpane.   
           createAndSelectNewTab(tabPane, "        "); 

           //Function to add and display new tabs with default URL display.  
           tabPane.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>() {  
                @Override  
                public void changed(ObservableValue<? extends Tab> observable,  
                          Tab oldSelectedTab, Tab newSelectedTab) {  
                     if (newSelectedTab == newtab) {  
                          Tab tab = new Tab();

                          //WebView - to display, browse web pages.  
                           WebView browser = new WebView();  
                          final WebEngine webEngine = browser.getEngine(); 
                          webEngine.load(DEFAULT_URL);  

                   EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {  
                               @Override public void handle(ActionEvent event) {  
                                    webEngine.load(urlField.getText().startsWith("http://")   
                                              ? urlField.getText()   
                                                        : "http://" + urlField.getText());  
                               }  
                          };  
                          urlField.setOnAction(goAction);

                          final VBox vBox = new VBox(5);  
                          vBox.getChildren().setAll(browser);  
                          VBox.setVgrow(browser, Priority.ALWAYS);  
                          tab.setContent(vBox); 
                          final ObservableList<Tab> tabs = tabPane.getTabs();  
                          tab.closableProperty().bind(Bindings.size(tabs).greaterThan(2));  
                          tabs.add(tabs.size() - 1, tab);  
                          tabPane.getSelectionModel().select(tab);  
                     }  
                }  
           }); 

//OnClick handling HomeB
        HomeB.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event){
                webEngine.load(DEFAULT_URL);
            }
        });

        toolbarMain.getChildren().addAll(HomeB,urlField);

//Placement of elements in borderpane
        borderPane.setCenter(tabPane);
        borderPane.setTop(toolbarMain);
        getChildren().add(borderPane);  
      }

}

Solution

  • When you click on the HomeB the default URL is loaded into the browser, a global WebView. That works, but you don't see the URL loaded, because you haven't added this browser to any of your tabs.

    Assuming you create the first tab for the home button:

    tabPane.getTabs().addAll(newtab); // tab 0, then moves to 1
    
    // Here you create a new tab, but put it on the 0 index:
    createAndSelectNewTab(tabPane, "        "); 
    
    // You can add now your global browser to the first tab:
    
    final VBox vBoxIni = new VBox(5);  
    vBoxIni.getChildren().setAll(browser);  
    VBox.setVgrow(browser, Priority.ALWAYS);  
    tabPane.getTabs().get(0).setContent(vBoxIni);  
    

    Other option will be using the local webView you have created for each tab, and load the default URL on the active tab:

    HomeB.setOnAction(new EventHandler<ActionEvent>(){
        @Override
        public void handle(ActionEvent event){
            VBox vBox=(VBox)tabPane.getSelectionModel().getSelectedItem().getContent();
            if(vBox!=null){
                WebView webView=(WebView)vBox.getChildren().get(0);
                webView.getEngine().load(DEFAULT_URL);
            }
        }
    });
    

    Note this won't work on the first tab, since you haven't set any content there.