WebEngine未在按钮单击时加载URL

时间:2015-01-15 11:13:11

标签: javafx javafx-8 javafx-webengine

我正在编写JAVAFx中的Tabbed Web浏览器。我面临的问题是: - 当我单击主页按钮(HomeB)时,它没有在当前选项卡中加载DEFAULT_URL。这是我的代码的一些有用的部分。有些人请修理它。谢谢

        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);  
      }

}

1 个答案:

答案 0 :(得分:0)

当您点击HomeB时,默认网址会加载到browser,即全局WebView。这样做有效,但您没有看到加载的URL,因为您尚未将此浏览器添加到任何标签页。

假设您为主页按钮创建了第一个标签:

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);  

其他选项将使用您为每个标签创建的本地webView,并在活动标签上加载默认网址:

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);
        }
    }
});

请注意,这不适用于第一个标签页,因为您尚未在其中设置任何内容。