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.
Related
I am trying to learn JavaFX and just stumbled across something I'm confused about. I created a new project in IntelliJ and added a single button.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
private Button firstButton;
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
firstButton = new Button("My First Button");
StackPane layout = new StackPane();
layout.getChildren().add(firstButton);
Scene scene = new Scene(layout, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
When I create the button with text it adds symbols instead of the text.
Window with "My First Button" as commanded text.
When I change the text to something small (I was thinking that it was erroring if the text was longer than the button length) it got even stranger. This is the next example I tried:
firstButton = new Button("E");
And the text I got was: Window with "E" as commanded text.
I have Googled a bunch and have not found anyone talking about this. Either I'm a bad Googler or it's not a common problem. My initial thought was that it was an encoding issue. After I saw the mystery E -> G though I'm thinking there might be something else off.
Any ideas what is going on?
Thank you!
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.
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
I am developing a chat application on JavaFX and I stuck with a little problem. In short, I want to have a possibility to insert an image(for example smiles) inside TextField which is intended for text input.But as I know, there is no possibility to insert images inside this element. Does JavaFX have some alternatives for that or maybe you know something else, what I can use for this?
Thank you, in advance.
You can try to use HTMLEditor. It supports styled text and other html features, which include adding images.
There is example of using HTMLEditor as inputField for chat:
package org.laptech.sample;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;
public class ChatFieldSample extends Application{
private HTMLEditor htmlEditor;
public void hideHTMLEditorToolbars() {
htmlEditor.setVisible(false);
Platform.runLater(() -> {
Node[] nodes = htmlEditor.lookupAll(".tool-bar").toArray(new Node[0]);
for (Node node : nodes) {
node.setVisible(false);
node.setManaged(false);
}
htmlEditor.setVisible(true);
});
}
#Override public void start(Stage primaryStage) throws Exception {
htmlEditor = new HTMLEditor();
hideHTMLEditorToolbars();
htmlEditor.setHtmlText("Text<img src='urltosmile'/>Text Text Text");
primaryStage.setScene(new Scene(htmlEditor, 300, 300));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I create htmlEditor and hide toolbar in method(hideHTMLEditorToolbars)
The only, which I faced , was the problem of losing focus, when i handle keyEvents and setText there.
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