WebView in JavaFX does not show the webpage - java

I'm working on a project. In that project I have to show a web page in JavaFX GUI. But it doesn't work out. It shows only a white window. My net connection is on.
Can anyone give suggestions what can I do to show a web page in my JavaFX GUI?
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) {
stage.setTitle("HTML");
stage.setWidth(500);
stage.setHeight(500);
Scene scene = new Scene(new Group());
VBox root = new VBox();
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
Hyperlink hpl = new Hyperlink("google.com");
hpl.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent e) {
webEngine.load("http://google.com");
}
});
root.getChildren().addAll(hpl,browser);
scene.setRoot(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Update: I replaced " http:// google.com" to "https:// google.com " . Then it works perfectly.

Your code work perfectly on my computer when i click, google page is displayed.
You can also try to load it without clicking.
It must be a network problem, i guess the proxy: add theses arguments to your launch:
-Dhttp.proxyHost=youriphost
-Dhttp.proxyPort=8080
It's to be added on command line if you launch from command line, or vm arguments if you launch from eclipse.

Related

When I run JavaFx class on my Mac, no window appears but a java icon appears in my dock

Can someone help please, when I run a JavaFx class on my Mac, no window appears but a java icon appears in my dock. When I click to show this file in finder, I see a file called java- Unix executable. I have no idea what is wrong, I have tried so many tutorials.
I am running this program on eclipse with jdk 12 and JavaFx13.
This is my code:
package JavaFx;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class JavaFx extends Application {
Button button;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage lol) throws Exception {
lol.setTitle("Title");
button = new Button();
button.setText("Click me");
StackPane layout = new StackPane();
layout.getChildren().add(button);
Scene scene = new Scene(layout, 300, 250);
lol.setScene(scene);
lol.show();
}
}

Using WebView in JavaFX breaks the UI on Linux

I would like to have a webview component and a button on the stage but when I load a http://mail.google.com in the WebView the other button drawn on the stage gets problems.
After having loaded the page successfully if I hover on the button with the mouse the button itself disappears leaving only its label visible.
The problem is only on Linux since when I tried the same project with Windows it was okay.
The funny story is that if I change the url I want to load with another one, say http://www.google.com the button is not affected by this weird problem.
My Linux 64 bit Mint 17.3 and the Java version is jdk1.8.0_91.
Here is the code, a video and some screenshots of the strange behaviour.
package javafxwebviewnofxml;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
/**
*
* #author Aldo Marx
*/
public class JavaFXWebviewNoFXML extends Application {
public WebEngine webEngine;
public String urlToGo;
public AnchorPane anchorPane;
private WebView webView;
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World my friends!");
}
});
anchorPane = new AnchorPane() ;
anchorPane.setPrefSize(1160, 900);
AnchorPane.setLeftAnchor(btn, 900.00);
AnchorPane.setTopAnchor(btn, 400.00);
webView = new WebView();
webView.setPrefSize(1160, 900);
webEngine = webView.getEngine();
urlToGo = "http://mail.google.com"; // this url is not okay
// urlToGo = " http://facebook.com"; // this url is okay
// urlToGo = "http://www.google.com"; // this url is okay
webEngine.load(urlToGo);
anchorPane.getChildren().addAll(webView);
anchorPane.getChildren().addAll(btn);
Scene scene = new Scene(anchorPane, 1160, 900);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The button as appears on the stage when it's not hovered by the mouse can be seen here below
The button as appears on the stage when it is hovered by the mouse (and this is the problem ) can be seen here below
I fixed the issue by adding -Dprism.order=j2d to VM options.
Don's set layout x, ... when placing in an AnchorPane, use anchoring: AnchorPane.setLeftAnchor and similar

URL to webview through JavaFX except we get our url data from another class?

Say You have a class with the JavaFX start().
This class is launched by another class, with main. I want to get information from the class with main, to the class with JavaFX. For some reason, a constructor won't work.
Here's the code that does the webview:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class htmlRender extends Application {
#Override
public void start(Stage stage) {
stage.setTitle("HTML");
stage.setWidth(500);
stage.setHeight(500);
Scene scene = new Scene(new Group());
VBox root = new VBox();
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
Hyperlink hpl = new Hyperlink("LINK");
hpl.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
webEngine.load("LINK");
}
});
root.getChildren().addAll(hpl, browser);
scene.setRoot(root);
stage.setScene(scene);
stage.show();
}
}
Where it says "LINK", I want to insert a string from another class.
The other class has
public static void main(String[]args){
htmlRender.launch(htmlRender.class, args);
}
JavaFX application launch parameters can be accessed via the Application.getParameters() method.

Sending notification with javafx without stage

You can send a desktop notification with JavaFx like this (requires jdk 8u20 or later):
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import org.controlsfx.control.Notifications;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
// Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Button notifyButton = new Button("Notify");
notifyButton.setOnAction(e -> {
Notifications.create().title("Test").text("Test Notification!").showInformation();
});
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(notifyButton, 100, 50));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
But this way you have to create a main window (stage), is it possible to avoid this? I am looking for a way to send notification like using zenity in bash: most of the code is non-gui, but uses some gui elements for informing or interacting with user in a very basic fashion.
It looks like the ControlsFX notifications require a existing stage. You can create a hidden utility stage. Try something like this.
package sample;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.controlsfx.control.Notifications;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
}
public static void main(String[] args) {
new JFXPanel();
notifier("Good!", "It's working now!");
}
private static void notifier(String pTitle, String pMessage) {
Platform.runLater(() -> {
Stage owner = new Stage(StageStyle.TRANSPARENT);
StackPane root = new StackPane();
root.setStyle("-fx-background-color: TRANSPARENT");
Scene scene = new Scene(root, 1, 1);
scene.setFill(Color.TRANSPARENT);
owner.setScene(scene);
owner.setWidth(1);
owner.setHeight(1);
owner.toBack();
owner.show();
Notifications.create().title(pTitle).text(pMessage).showInformation();
}
);
}
}
new JFXPanel() initializes the JavaFX thread without having to extend Application. You have to call this before any calls to Platform.runLater() otherwise you will get a thread exception. You only need to call it once for the whole application though. Honestly it is probably better to create your own notification stage and display it directly. Create a stage like above and put your own contents. You can probably reuse some of the styling from the ControlsFX source.

Disabling loading of images in JavaFX WebEngine

I've searched everywhere but found no way to disable loading of images by Java WebEngine.
Research done:
I found some ideas, such as using URL.setURLStreamHandlerFactory() to use my own URLStreamHandler, and having that analyze the URL to only return URLConnections for URL's that don't end in .jpg .png etc. BUt that has many problems: Sometimes the image url doesn't end in .jpg, if it's a dynamic image, such as a captcha. So how can I disable automatic image loading from WebEngine?
I'm no JSoup expert but it should be easy enough to do something like this.
import java.net.URL;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class NoImg extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
WebView wv = new WebView();
WebEngine we = wv.getEngine();
Document doc = Jsoup.parse(new URL("http://www.google.com"), 5000);
doc.select("img").stream().forEach((element) -> {
element.remove();
});
we.loadContent(doc.outerHtml());
Scene scene = new Scene(wv, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
It seems to work fine. You need the jsoup.jar

Categories

Resources