Disabling loading of images in JavaFX WebEngine - java

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

Related

JavaFX WebView: custom cursors not working?

I tried to get css custom cursors to work with Java WebView within a tag, to no avail.
For example:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) {
primaryStage.setTitle("JavaFX WebView Example");
WebView webView = new WebView();
String cursorUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8c/Pixel_51_icon_cursor_click_top_right.svg/36px-Pixel_51_icon_cursor_click_top_right.svg.png";
String content = String.format("<body style=cursor: url('%s'), auto;>", cursorUrl);
content = content + "<br>some text<br> a link: http://google.com </body>";
System.out.println(content);
webView.getEngine().loadContent(content);
VBox vBox = new VBox(webView);
Scene scene = new Scene(vBox, 960, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
which just shows the regular cursor.
I also tried to replace the .png cursor by a .cur file, as well as remove the quotes around the url. Nothing seems to work.
Does WebView not support the feature? Other cursor such as wait and grab work fine.
It was a mere problem related to quotes.
I changed the content line to
String content = String.format("<body style=\"cursor: url('%s') 10 10, auto\";>", cursorUrl);
and it worked fine.

Crop a part of an image ( javafx )

I am new to overflow so I apologize in advance if I am not precise enough.
During a class project I have to make a tanquin game in javafx. I must be able to cut an image into x part in order to insert them then in an Arraylist with an id and the part of the image. But I have the error attached here.
I guess the reader is null and that the error is related to that. But I don't know how to fix it.
I never used those library so i dont know how to use them.
Thank you in advance for your reply.
Code:
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Image image = new Image("https://scontent-yyz1-1.cdninstagram.com/v/t51.2885-15/sh0.08/e35/c180.0.720.720a/s640x640/75272157_461406551233157_7963091763107249286_n.jpg?_nc_ht=scontent-yyz1-1.cdninstagram.com&_nc_cat=105&_nc_ohc=eu-9K8wyx6oAX9H695F&oh=429cc1e6dcec03badc9aac439b6b8ac0&oe=5EAD8EA3", true);
PixelReader reader = image.getPixelReader();
WritableImage newImage = new WritableImage(reader,100,100,100,100);
ImageView imageView = new ImageView(newImage);
HBox hbox = new HBox(imageView);
Scene scene = new Scene(hbox, 640, 640);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

WebView in JavaFX does not show the webpage

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.

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.

Categories

Resources