0

I tried displaying the google web page on my javafx view using webview. All it does is display an empty page. For testing I did add a text element at the bottom and it did show up. Any pointers would be helpful. My code and the sample screen are attached.

I am running this application on a Windows 7 machine with 8 GB RAM and this is deployed in an environment that needs proxy authentication.

import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class MyBrowser extends Application
{

private Pane root;

@Override
public void start(final Stage stage) throws URISyntaxException
{
    root = new VBox();

    List<Proxy> proxies = ProxySelector.getDefault().select(new URI("http://www.google.com"));
    final Proxy proxy = proxies.get(0); // ignoring multiple proxies to simplify code snippet
    if (proxy.type() != Proxy.Type.DIRECT)
    {
        // you can change that to dialog using separate Stage
        final TextField login = new TextField("login");
        final PasswordField pwd = new PasswordField();
        Button btn = new Button("Login");
        btn.setOnAction(new EventHandler<ActionEvent>()
        {
            public void handle(ActionEvent t)
            {
                System.setProperty("http.proxyUser", login.getText());
                System.setProperty("http.proxyPassword", pwd.getText());
                displayWebView();
            }
        });
        root.getChildren().addAll(login, pwd, btn);
    }
    else
    {
        displayWebView();
    }

    stage.setScene(new Scene(root, 400, 600));
    stage.show();
}

private void displayWebView()
{
    root.getChildren().clear();
    WebView webView = new WebView();

    final WebEngine webEngine = webView.getEngine();
    root.getChildren().addAll(webView, new Text("HELLO"));
    webEngine.load("http://www.google.com");

}

public static void main(String[] args)
{
    launch();
}
}

Empty screen on running the application

Aspirant
  • 1,934
  • 4
  • 25
  • 44
  • possible duplicate of [Autodetect proxy - JavaFX - webview](http://stackoverflow.com/questions/15575276/autodetect-proxy-javafx-webview) – jewelsea Oct 10 '13 at 19:33

1 Answers1

0

I copied and pasted your code and ran it on Windows 7 with Java7u40 both Java8b108.

In both cases the code functioned correctly and displayed the http://www.google.com page.

The proxy selector code in your source was not triggered for me (probably because I have a Proxy.Type.DIRECT connection, so there was nothing for it to do).

google

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • This works in an environment that does not have a proxy setup. But still, the problem remains that this not work where proxy authentication is required. – Aspirant Oct 10 '13 at 19:03
  • As your clarified question seems to be purely around proxy setup with JavaFX, then I have voted to close it as a duplicate of [Autodetect proxy - JavaFX - webview](http://stackoverflow.com/questions/15575276/autodetect-proxy-javafx-webview). – jewelsea Oct 10 '13 at 19:35
  • Agree with you. Closing the discussion. I also think that a popup like [this](http://www.blogosfera.co.uk/wp-content/plugins/wp-o-matic/cache/784a9d9732_djUpW.jpg) would appear when a javafx application needs proxy settings and this code should not be used to manually ask the user to enter proxy details. – Aspirant Oct 10 '13 at 19:50