JSException in Javafx webview - java

I'm getting some weird behaviour that I don't understand. I have a class MonologueTab:
public final class MonologueTab extends VBox{
private MonologueNode node;
WebView webView;
WebEngine webEngine;
public MonologueTab(){
this.node = Game.getInstance().getMonologueDatabase().getMonologueNode();
webView = new WebView();
webEngine = webView.getEngine();
createGui();
}
public void createGui(){
Label textLabel = new Label ();
textLabel.setGraphic(webView);
textLabel.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
webEngine.loadContent(node.getNextLine());
this.getChildren().add(textLabel);
textLabel.setOnMouseClicked((MouseEvent event) -> {
String nextLine = node.getNextLine();
if (nextLine!=null){
webEngine.executeScript("document.getElementById('content').insertAdjacentHTML( 'beforeend'," + nextLine + ");");
webEngine.executeScript("window.scrollTo(0, document.body.scrollHeight);");
}
else{
EventManager.getInstance().notifyObserver("end" + node.getStartTrigger());
}
});
}
}
that works as expected. But when I try to port basically the same code in to a new class DialogueTab:
public final class DialogueTab extends VBox implements EventObserver{
WebView webView;
WebEngine webEngine;
public DialogueTab(){
webView = new WebView();
webEngine = webView.getEngine();
EventManager.getInstance().addObserver(this);
createGui();
}
public void createGui(){
this.getChildren().removeAll(this.getChildren());
Label conversation = new Label();
conversation.setGraphic(webView);
conversation.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
webEngine.loadContent("<body><b>Available Conversations</b><br><div id='content'></div></body>");
this.getChildren().add(conversation);
for (Person p: Game.getInstance().getPeopleAt()){
webEngine.executeScript("document.getElementById('content').insertAdjacentHTML('beforeend', " + p.getShortDesc() + ");");
//some irrelevance removed
}
}
It's throwing a:
netscape.javascript.JSException: TypeError: null is not an object (evaluating 'document.getElementById('content').insertAdjacentHTML')
Now, I'm assuming that this is related to the issue here but I don't understand why it's working fine in one place but not in another.

I think why the first example works is because the scripts are only executed upon mouseclick (by which time the page should be fully loaded); While the second example execute the scripts direcly after it has requested to start loading (which is so fast after starting that it can't possibly be finnished loading).
As for the solution, you provided the answer yourself in the post you linked. If you need any further explanation, feel free to ask.

Related

java.lang.IllegalStateException: Application launch must not be called more than once - JavaFX (first once works, the 2nd non-) [duplicate]

How to call the launch() more than once in java i am given an exception as "ERROR IN MAIN:java.lang.IllegalStateException: Application launch must not be called more than once"
I have create rest cleint in my java application when request comes it call javafx and opening webview after completing webview operarion am closing javafx windows using Platform.exit() method. when second request comes am getting this error how to reslove this error.
JavaFx Application Code:
public class AppWebview extends Application {
public static Stage stage;
#Override
public void start(Stage _stage) throws Exception {
stage = _stage;
StackPane root = new StackPane();
WebView view = new WebView();
WebEngine engine = view.getEngine();
engine.load(PaymentServerRestAPI.BROWSER_URL);
root.getChildren().add(view);
engine.setJavaScriptEnabled(true);
Scene scene = new Scene(root, 800, 600);
stage.setScene(scene);
engine.setOnResized(new EventHandler<WebEvent<Rectangle2D>>() {
public void handle(WebEvent<Rectangle2D> ev) {
Rectangle2D r = ev.getData();
stage.setWidth(r.getWidth());
stage.setHeight(r.getHeight());
}
});
JSObject window = (JSObject) engine.executeScript("window");
window.setMember("app", new BrowserApp());
stage.show();
}
public static void main(String[] args) {
launch(args);
}
RestClient Method:
Calling to JavaFX application
// method 1 to lanch javafx
javafx.application.Application.launch(AppWebview.class);
// method 2 to lanch javafx
String[] arguments = new String[] {"123"};
AppWebview .main(arguments);
You can't call launch() on a JavaFX application more than once, it's not allowed.
From the javadoc:
It must not be called more than once or an exception will be thrown.
Suggestion for showing a window periodically
Just call Application.launch() once.
Keep the JavaFX runtime running in the background using Platform.setImplicitExit(false), so that JavaFX does not shutdown automatically when you hide the last application window.
The next time you need another window, wrap the window show() call in Platform.runLater(), so that the call gets executed on the JavaFX application thread.
For a short summary implementation of this approach:
See the answer by sergioFC
If you are mixing Swing you can use a JFXPanel instead of an Application, but the usage pattern will be similar to that outlined above.
For an example of the JFXPanel apprach, see Irshad Babar
s answer.
Wumpus Sample
This example is bit more complicated than it needs to be because it also involves timer tasks. However it does provide a complete stand-alone example, which might help sometimes.
import javafx.animation.PauseTransition;
import javafx.application.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.*;
// hunt the Wumpus....
public class Wumpus extends Application {
private static final Insets SAFETY_ZONE = new Insets(10);
private Label cowerInFear = new Label();
private Stage mainStage;
#Override
public void start(final Stage stage) {
// wumpus rulez
mainStage = stage;
mainStage.setAlwaysOnTop(true);
// the wumpus doesn't leave when the last stage is hidden.
Platform.setImplicitExit(false);
// the savage Wumpus will attack
// in the background when we least expect
// (at regular intervals ;-).
Timer timer = new Timer();
timer.schedule(new WumpusAttack(), 0, 5_000);
// every time we cower in fear
// from the last savage attack
// the wumpus will hide two seconds later.
cowerInFear.setPadding(SAFETY_ZONE);
cowerInFear.textProperty().addListener((observable, oldValue, newValue) -> {
PauseTransition pause = new PauseTransition(
Duration.seconds(2)
);
pause.setOnFinished(event -> stage.hide());
pause.play();
});
// when we just can't take it anymore,
// a simple click will quiet the Wumpus,
// but you have to be quick...
cowerInFear.setOnMouseClicked(event -> {
timer.cancel();
Platform.exit();
});
stage.setScene(new Scene(cowerInFear));
}
// it's so scary...
public class WumpusAttack extends TimerTask {
private String[] attacks = {
"hugs you",
"reads you a bedtime story",
"sings you a lullaby",
"puts you to sleep"
};
// the restaurant at the end of the universe.
private Random random = new Random(42);
#Override
public void run() {
// use runlater when we mess with the scene graph,
// so we don't cross the streams, as that would be bad.
Platform.runLater(() -> {
cowerInFear.setText("The Wumpus " + nextAttack() + "!");
mainStage.sizeToScene();
mainStage.show();
});
}
private String nextAttack() {
return attacks[random.nextInt(attacks.length)];
}
}
public static void main(String[] args) {
launch(args);
}
}
Update, Jan 2020
Java 9 added a new feature called Platform.startup(), which you can use to trigger startup of the JavaFX runtime without defining a class derived from Application and calling launch() on it. Platform.startup() has similar restrictions to the launch() method (you cannot call Platform.startup() more than once), so the elements of how it can be applied is similar to the launch() discussion and Wumpus example in this answer.
For a demonstration on how Platform.startup() can be used, see Fabian's answer to How to achieve JavaFX and non-JavaFX interaction?
I use something like this, similar to other answers.
private static volatile boolean javaFxLaunched = false;
public static void myLaunch(Class<? extends Application> applicationClass) {
if (!javaFxLaunched) { // First time
Platform.setImplicitExit(false);
new Thread(()->Application.launch(applicationClass)).start();
javaFxLaunched = true;
} else { // Next times
Platform.runLater(()->{
try {
Application application = applicationClass.newInstance();
Stage primaryStage = new Stage();
application.start(primaryStage);
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
try this, I tried this and found successful
#Override
public void start() {
super.start();
try {
// Because we need to init the JavaFX toolkit - which usually Application.launch does
// I'm not sure if this way of launching has any effect on anything
new JFXPanel();
Platform.runLater(new Runnable() {
#Override
public void run() {
// Your class that extends Application
new ArtisanArmourerInterface().start(new Stage());
}
});
} catch (Exception e) {
e.printStackTrace();
}
}

How do I load live DOM in Java Application?

I'm not really sure how to ask this question but here is what I am trying to do. I am looking for the ability to load a website in java application and being able to click the buttons, text, box, etc and get the underlying DOM code.
For example:
-It loads google.com the webpage as well as the LIVE DOM under it, different frame. It can't be HTML since google is javascript.
-I want to click the google search box, or anything on the page, and application will print any attribute, such as name="q" or just q.
I've looked into XULrunner but it is deprecated for java.
You can use JavascriptExecutor for that. It is implemented by all WebDrivers. See JavaDoc for details. Use documentto refer to the DOM.
You can use JavaFX WebView.
You can call back to your Java code from Javascript. So you could write a small amount of Javascript that detects when the user clicked on element, and call some Java code that shows attribute value, for example. See "Calling back to Java from Javascript" in the WebEngine API docs.
Sample code:
public class HTMLMouseOverTest extends Application {
#Override
public void start(Stage primaryStage) {
final BorderPane root = new BorderPane();
final WebView webView = new WebView();
final WebEngine engine = webView.getEngine();
final BooleanProperty mouseOver = new SimpleBooleanProperty();
engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
#Override
public void changed(ObservableValue<? extends State> observable,
State oldValue, State newValue) {
if (newValue == State.SUCCEEDED) {
// Here's how to add the Javascript if you don't have
// direct access to the HTML:
// final Document doc = engine.getDocument();
// Element div = doc.getElementById("important-div");
// div.setAttribute("onmouseover", "mouseOverProperty.set(true)");
// div.setAttribute("onmouseout", "mouseOverProperty.set(false)");
final JSObject window = (JSObject) engine.executeScript("window");
window.setMember("mouseOverProperty", mouseOver);
}
}
});
engine.loadContent("<html><body style='font-family:sans-serif';><h2>Hello World</h2>"+
"<div id='important-div' onmouseover='mouseOverProperty.set(true)'"+
"onmouseout='mouseOverProperty.set(false)' style='background: #ffd; padding:40px;'>"+
"Move mouse here</div>"+
"<h3>Thanks and good night</h3></body></html>");
root.setCenter(webView);
final Label label = new Label();
label.textProperty().bind(Bindings.when(mouseOver).then("Mouse in position").otherwise("Mouse out of area"));
root.setBottom(label);
final Scene scene = new Scene(root, 400, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}

javafx snapshot without showing application or Scene

Hi i'm using JavaFx WebView to create Screenshot of HTML pages and it works fine but i wanted to know is it possible to do this without launching the application in Graphical Windows!! I mean aren't there any more lightweight method to get the screenshot then this:
public class WebViewSample extends Application {
private Scene scene;
#Override
public void start(Stage stage) {
// create scene
scene = new Scene(new Browser(snapshot), 750, 500, Color.web("#666970"));
stage.setScene(scene);
// show stage
stage.show();
}
WritableImage snapshot;
public static void main(String[] args) {
launch(args);
System.err.println("launched!");
}
}
class Browser extends Region {
final ImageView selectedImage = new ImageView();
final WebView browser = new WebView();
final WebEngine webEngine = browser.getEngine();
private final WritableImage snapshotImage;
public Browser(WritableImage snapshot) {
this.snapshotImage= snapshot;
// process page loading
webEngine.getLoadWorker().stateProperty().addListener(
new ChangeListener<State>() {
#Override
public void changed(ObservableValue<? extends State> ov,
State oldState, State newState) {
if (newState == State.SUCCEEDED) {
WritableImage newSnapshot = browser.snapshot(null, snapshotImage);
File file = new File("test2.png");
RenderedImage renderedImage = SwingFXUtils.fromFXImage(newSnapshot, null);
try {
ImageIO.write(renderedImage, "png", file);
} catch (IOException e1) {
e1.printStackTrace();
}
System.exit(0);
}
}
}
);
// load the home page
webEngine.load("http://localhost/");
//add components
getChildren().add(browser);
}
}
For JavaFX 2.2 and below, there's not functionality of the kind.
Headless JavaFX applications are currently not possible, and a main JavaFX thread is mandatory.
The best you can do is read upon several workarounds for achieving this.
Related StackOverflow questions:
JavaFX for server-side image generation
Generating image at server side using Java FX
JavaFx in headless mode
How to test JavaFX 2 in a headless environment?

Java GUI to display webpages and return HTML

I need a workflow like below:
// load xyz.com in the browser window
// the browser is live, meaning users can interact with it
browser.load("http://www.google.com");
// return the HTML of the initially loaded page
String page = browser.getHTML();
// after some time
// user might have navigated to a new page, get HTML again
String newpage = browser.getHTML();
I am surprised to see how hard this is to do with Java GUIs such as JavaFX (http://lexandera.com/2009/01/extracting-html-from-a-webview/) and Swing.
Is there some simple way to get this functionality in Java?
Here is a contrived example using JavaFX that prints the html content to System.out - it should not be too complicated to adapt to create a getHtml() method. (I have tested it with JavaFX 8 but it should work with JavaFX 2 too).
The code will print the HTML content everytime a new page is loaded.
Note: I have borrowed the printDocument code from this answer.
public class TestFX extends Application {
#Override
public void start(Stage stage) throws Exception {
try {
final WebView webView = new WebView();
final WebEngine webEngine = webView.getEngine();
Scene scene = new Scene(webView);
stage.setScene(scene);
stage.setWidth(1200);
stage.setHeight(600);
stage.show();
webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
#Override
public void changed(ObservableValue<? extends State> ov, State t, State t1) {
if (t1 == Worker.State.SUCCEEDED) {
try {
printDocument(webEngine.getDocument(), System.out);
} catch (Exception e) { e.printStackTrace(); }
}
}
});
webView.getEngine().load("http://www.google.com");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}
public static void main(String[] args) {
launch(args);
}
}
Below you will find a SimpleBrowser component which is a Pane containing a WebView.
Source code at gist.
Sample usage:
SimpleBrowser browser = new SimpleBrowser()
.useFirebug(true);
// ^ useFirebug(true) option - will enable Firebug Lite which can be helpful for
// | debugging - i.e. to inspect a DOM tree or to view console messages
Scene scene = new Scene(browser);
browser.load("http://stackoverflow.com", new Runnable() {
#Override
public void run() {
System.out.println(browser.getHTML());
}
});
browser.getHTML() is put inside a Runnable because one needs to wait for a web page to download and render. Trying to invoke this method before page loading will return an empty page, so wrapping this into a runnable is a simple way I came up with to wait for a page to load.
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.scene.layout.*;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
public class SimpleBrowser extends Pane {
protected final WebView webView = new WebView();
protected final WebEngine webEngine = webView.getEngine();
protected boolean useFirebug;
public WebView getWebView() {
return webView;
}
public WebEngine getEngine() {
return webView.getEngine();
}
public SimpleBrowser load(String location) {
return load(location, null);
}
public SimpleBrowser load(String location, final Runnable onLoad) {
webEngine.load(location);
webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
#Override
public void changed(ObservableValue<? extends Worker.State> ov, Worker.State t, Worker.State t1) {
if (t1 == Worker.State.SUCCEEDED) {
if(useFirebug){
webEngine.executeScript("if (!document.getElementById('FirebugLite')){E = document['createElement' + 'NS'] && document.documentElement.namespaceURI;E = E ? document['createElement' + 'NS'](E, 'script') : document['createElement']('script');E['setAttribute']('id', 'FirebugLite');E['setAttribute']('src', 'https://getfirebug.com/' + 'firebug-lite.js' + '#startOpened');E['setAttribute']('FirebugLite', '4');(document['getElementsByTagName']('head')[0] || document['getElementsByTagName']('body')[0]).appendChild(E);E = new Image;E['setAttribute']('src', 'https://getfirebug.com/' + '#startOpened');}");
}
if(onLoad != null){
onLoad.run();
}
}
}
});
return this;
}
public String getHTML() {
return (String)webEngine.executeScript("document.getElementsByTagName('html')[0].innerHTML");
}
public SimpleBrowser useFirebug(boolean useFirebug) {
this.useFirebug = useFirebug;
return this;
}
public SimpleBrowser() {
this(false);
}
public SimpleBrowser(boolean useFirebug) {
this.useFirebug = useFirebug;
getChildren().add(webView);
webView.prefWidthProperty().bind(widthProperty());
webView.prefHeightProperty().bind(heightProperty());
}
}
Demo Browser:
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.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.stage.Stage;
public class FXBrowser {
public static class TestOnClick extends Application {
#Override
public void start(Stage stage) throws Exception {
try {
SimpleBrowser browser = new SimpleBrowser()
.useFirebug(true);
final TextField location = new TextField("http://stackoverflow.com");
Button go = new Button("Go");
go.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
browser.load(location.getText(), new Runnable() {
#Override
public void run() {
System.out.println("---------------");
System.out.println(browser.getHTML());
}
});
}
});
HBox toolbar = new HBox();
toolbar.getChildren().addAll(location, go);
toolbar.setFillHeight(true);
VBox vBox = VBoxBuilder.create().children(toolbar, browser)
.fillWidth(true)
.build();
Scene scene = new Scene( vBox);
stage.setScene(scene);
stage.setWidth(1024);
stage.setHeight(768);
stage.show();
VBox.setVgrow(browser, Priority.ALWAYS);
browser.load("http://stackoverflow.com");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
}
There is not a simple solution. In fact, there might not even be a solution at all short of building your own browser.
The key issue is interaction. If you want to display content only, then JEditorPane and many third party libs make that a more attainable goal. If you really need a user interacting with a webpage then either:
Have the user use a normal browser to interact
Build a GUI that makes calls to web services/urls to do the interaction, but the display is up to you.
On the returning the HTML side of things, it sounds like you are trying to capture history or refresh the page. In either case, it sounds like you are in the wrong technology. Either modify the original site, or add in some java script in the browser with Greasemonkey or something similar.
You may want to see to djproject. But possibly you'll find JavaFX usage easier.
Depending on stuff I don't know about your project this is either genious or moronic, but you could use a real browser in stead and instrument it with Selenium Webdriver. Only suggesting this as it appears from the other answer that you are going down a difficult path.
There's another question about extracting html with webdriver here. It's about using python, but webdriver has a java api as well.
I was able to get the executed html. I kept the alert statement after the html is loaded in JavaScript. I used webEngine.setOnAlert method to check if the alert was executed and then printed the html. I got the correct response. Below is the code
HTML
alert("ready");
JavaFx Application
webEngine.setOnAlert(new EventHandler<WebEvent<String>>(){
#Override
public void handle(WebEvent<String> event) {
//labelWebTitle.setText(webEngine.getTitle());
if("ready".equals(event.getData())){
//TODO: initialize
System.out.println("HTML Ready");
WebEngine engine = (WebEngine)event.getSource();
String html = (String) engine.executeScript("document.documentElement.outerHTML");
org.jsoup.nodes.Document doc = Jsoup.parse(html);
Element image = doc.getElementById("canvasImage");
System.out.println(image.attr("src"));
}
}
});

Fill fields WebView in Java automatically

So here's my problem. I'm using WebView class from JavaFX in swing. The thing I want to do is that I want fields loaded in webview to be filled automatically with information stored in an array. Is it possible?
Thanks in advance
Here is an automated form fill example JavaFX app for WebView.
Values (login credentials) are entered into JavaFX fields in the yellow part of the screen and then automatically posted (using the w3c dom api) in the WebView (the white part of the screen) when the login page appears.
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.beans.value.*;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.web.*;
import javafx.stage.Stage;
import org.w3c.dom.*;
import org.w3c.dom.html.*;
public class WebViewFormPost extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
final TextField fxUsername = new TextField();
fxUsername.setPrefColumnCount(20);
final TextField fxPassword = new PasswordField();
final BooleanProperty loginAttempted = new SimpleBooleanProperty(false);
final WebView webView = new WebView();
webView.setPrefWidth(1000);
final WebEngine engine = webView.getEngine();
engine.documentProperty().addListener(new ChangeListener<Document>() {
#Override
public void changed(ObservableValue<? extends Document> ov, Document oldDoc, Document doc) {
if (doc != null && !loginAttempted.get()) {
if (doc.getElementsByTagName("form").getLength() > 0) {
HTMLFormElement form = (HTMLFormElement) doc.getElementsByTagName("form").item(0);
if ("/oam/server/sso/auth_cred_submit".equals(form.getAttribute("action"))) {
HTMLInputElement username = null;
HTMLInputElement password = null;
NodeList nodes = form.getElementsByTagName("input");
for (int i = 0; i < nodes.getLength(); i++) {
HTMLInputElement input = (HTMLInputElement) nodes.item(i);
switch (input.getName()) {
case "ssousername":
username = input;
break;
case "password":
password = input;
break;
}
}
if (username != null && password != null) {
loginAttempted.set(true);
username.setValue(fxUsername.getText());
password.setValue(fxPassword.getText());
form.submit();
}
}
}
}
}
});
engine.getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
#Override
public void changed(ObservableValue<? extends Throwable> ov, Throwable oldException, Throwable exception) {
System.out.println("Load Exception: " + exception);
}
});
GridPane inputGrid = new GridPane();
inputGrid.setHgap(10);
inputGrid.setVgap(10);
inputGrid.addRow(0, new Label("Username: "), fxUsername);
inputGrid.addRow(0, new Label("Password: "), fxPassword);
Button fxLoginButton = new Button("Login to Oracle Forums");
fxLoginButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
if (notEmpty(fxPassword.getText()) && notEmpty(fxPassword.getText())) {
loginAttempted.set(false);
engine.load("https://forums.oracle.com/community/developer/english/java/javafx/login.jspa");
}
}
});
fxLoginButton.setDefaultButton(true);
ProgressIndicator fxLoadProgress = new ProgressIndicator(0);
fxLoadProgress.progressProperty().bind(webView.getEngine().getLoadWorker().progressProperty());
fxLoadProgress.visibleProperty().bind(webView.getEngine().getLoadWorker().runningProperty());
HBox loginPane = new HBox(10);
loginPane.getChildren().setAll(
fxLoginButton,
fxLoadProgress
);
final VBox layout = new VBox(10);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
layout.getChildren().addAll(
new Label("Enter your Oracle Web Account credentials"),
inputGrid,
loginPane,
webView
);
VBox.setVgrow(webView, Priority.ALWAYS);
stage.setScene(new Scene(layout));
stage.show();
fxUsername.requestFocus();
}
private boolean notEmpty(String s) {
return s != null && !"".equals(s);
}
}
The above application is adapted from a previous Oracle forum question on Submitting HTML Forms with JavaFX Webview.
If you don't have an Oracle technology network account to test the above program, you can sign up for one here: https://myprofile.oracle.com/EndUser/faces/profile/createUser.jspx.
Posting to WebView using JQuery
An alternate implementation, that I would actually prefer is to use is JavaScript jQuery to introspect the DOM and perform the post rather than using the Java DOM apis. There is a sample for using jQuery on any arbitrary webpage hosted in a WebView. So you could combine the ideas from this automated WebView form post and the jQuery hosted WebView sample to create a version which uses JQuery to perform the post.
I fixed this with JavaFX webView Javascript engine.
If anyone is intersted here's code snippet.
String setLastName = "document.getElementsByName('lastName')[0].value='" + lastName + "';";
String setName = "document.getElementsByName('firstName')[0].value='" + name + "'";
String setDateBirth = "document.getElementsByName('birthdate')[0].value='" + datebirth + "';";
String setPhone = "document.getElementsByName('phone')[0].value='" + phone + "';";
String setEmail = "document.getElementsByName('email')[0].value='" + email + "';";
String setPassport = "document.getElementsByName('passport')[0].value='" + passport + "';";
Button button = new Button("Fill the form");
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
webEngine.executeScript(setLastName);
webEngine.executeScript(setName);
webEngine.executeScript(setDateBirth);
webEngine.executeScript(setPhone);
webEngine.executeScript(setEmail);
webEngine.executeScript(setPassport);
}
});

Categories

Resources