1

I am a newbie in JavaFX so I decided to make a simple web browser and it works fine but only with a predefined URL (http://google.com in this example). I did the following: created BrowserTop.fxml (which includes a TextField for URL and a button for getting this URL) and Browser.fxml which includes plain WebView. So, my problem is that I can't understand how can I pass a String URL to WebView (which uses Browser.fxml) by clicking the button in BrowserTop.fxml?

Browser.java

package browser;

import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;


public class Browser extends Application {
    private Stage stage;
    private BorderPane rootLayout;

    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;

        initBrowserTop();
        initBrowserMain();
    }


    public void initBrowserTop() throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Browser.class.getResource("BrowserTop.fxml"));
        rootLayout = (BorderPane) loader.load();

        Scene scene = new Scene(rootLayout);
        stage.setScene(scene);
        stage.show();
    }

    public void initBrowserMain() throws IOException{
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Browser.class.getResource("Browser.fxml"));

        WebView browserWebView = (WebView) loader.load();
        WebEngine myWebEngine = browserWebView.getEngine();
        myWebEngine.load("http://google.com");

        rootLayout.setCenter(browserWebView);
    }


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

BrowserTop.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHe

    ight="728.0" prefWidth="1024.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="browser.BrowserTopController">
       <top>
          <AnchorPane>
             <children>
                <TextField fx:id="txtURL" layoutX="52.0" prefHeight="25.0" prefWidth="584.0" AnchorPane.leftAnchor="52.0" AnchorPane.rightAnchor="0.0" BorderPane.alignment="CENTER" />
                <Button fx:id="btnGo" mnemonicParsing="false" onAction="#handleButtonAction" prefHeight="25.0" prefWidth="53.0" text="Go" AnchorPane.leftAnchor="0.0" />
             </children>
          </AnchorPane>
       </top>
    </BorderPane>

BrowserTopController.java

package browser;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;

public class BrowserTopController implements Initializable {

    @FXML TextField txtURL;

    @FXML
    public void handleButtonAction(ActionEvent event){
        //something should be here

    }


    /**
     * Initializes the controller class.
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}
Lautern
  • 37
  • 1
  • 8
  • See if the techniques in http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml help – James_D Dec 26 '14 at 16:04

1 Answers1

0

I have actually used the techniques referred above by @James_D before. I made your code work by doing this:

package browser;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;


public class Browser extends Application {
    private Stage stage;
    private BorderPane rootLayout;

    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Browser.class.getResource("BrowserTop.fxml"));
        rootLayout = (BorderPane) loader.load();
        BrowserTopController browserTopController = (BrowserTopController) loader.getController(); 

        WebView browserWebView = new WebView();
        WebEngine myWebEngine = browserWebView.getEngine();


        browserTopController.btnGo.setOnAction(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    myWebEngine.load(browserTopController.txtURL.getText());
                }
            });

        rootLayout.setCenter(browserWebView);

        Scene scene = new Scene(rootLayout);
        stage.setScene(scene);
        stage.show();
    }



    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

You also have to add

    @FXML Button btnGo;

to your fxml document.

RonSiven
  • 939
  • 2
  • 10
  • 23
  • it works like a charm!! Thank you!! Due to my lack of experience I couldn't get something out of James_D's link, but now I will investigate it one more time with a working example in hands – Lautern Dec 29 '14 at 03:04