webview取java的参数值,在JavaFX WebView中获取HTML输入值?

Is it possible to get the value of a HTML input inside a WebView in JavaFX?

If so, how would I use an event so the value updates automatically?

Example App:

package webviewinputvalue;

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.layout.StackPane;

import javafx.scene.web.WebEngine;

import javafx.scene.web.WebView;

import javafx.stage.Stage;

public class WebviewInputValue extends Application {

@Override

public void start(Stage primaryStage) {

WebView test = new WebView();

WebEngine run = test.getEngine();

run.loadContent("");

StackPane root = new StackPane();

root.getChildren().add(test);

Scene scene = new Scene(root, 300, 250);

primaryStage.setTitle("Hello World!");

primaryStage.setScene(scene);

primaryStage.show();

}

public static void main(String[] args) {

launch(args);

}

}

Which creates a simple window like this:

MTWU2.png

Question: Is it possible to get the value of the textarea using JavaFX? If so, how should I do it?

解决方案

TL;DR: Yes, it's possible, but it requires a bit of a hacky workaround.

First of all, I'm going to move the HTML to it's own separate file, to make editing easier, and just create cleaner code in general.

editor.html

Example Live-view

document.querySelector("textarea").addEventListener("keyup", function(){

window.status = this.value;

});

The basic structure is really simple, but what about the element? What does it do?

document.querySelector("textarea").addEventListener("keyup", function(){

window.status = this.value;

});

AFAIK, it isn't possible to straight out get the value of an input in JavaFX, so you have to make a workaround. In this case, I'm setting the window status with the textarea value, whenever there is a keyup event in the textarea.

Now we need to set up the webview in our JavaFX.

WebviewInputValue.java

package webviewinputvalue;

public class WebviewInputValue extends Application {

private WebView InitWebview(){

//Create browser

WebView browser = new WebView();

WebEngine render = browser.getEngine();

//Load simple HTML

String editor = WebviewInputValue.class.getResource("editor.html").toExternalForm();

render.load(editor);

//Listen for state change

render.getLoadWorker().stateProperty().addListener((ov, o, n) -> {

if (Worker.State.SUCCEEDED == n) {

render.setOnStatusChanged(webEvent -> {

//Call value change

onValueChange(webEvent.getData());

});

}

});

return browser;

}

//called when value changes

private void onValueChange(String data){

//Print out data

System.out.println(data);

//If the data is equal to "exit", close the program

if("exit".equals(data)){

//Print goodbye message

System.out.println("Received exit command! Goodbye :D");

//Exit

System.exit(0);

}

}

@Override

public void start(Stage primaryStage) {

//Create browser

WebView browser = InitWebview();

StackPane root = new StackPane();

root.getChildren().add(browser);

Scene scene = new Scene(root, 300, 250);

primaryStage.setTitle("Hello World!");

primaryStage.setScene(scene);

primaryStage.show();

}

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

launch(args);

}

}

Again, very simple code. Let's dig into it:

private WebView InitWebview(){

//Create browser

WebView browser = new WebView();

WebEngine render = browser.getEngine();

//Load simple HTML

String editor = WebviewInputValue.class.getResource("editor.html").toExternalForm();

render.load(editor);

//Listen for state change

render.getLoadWorker().stateProperty().addListener((ov, o, n) -> {

if (Worker.State.SUCCEEDED == n) {

render.setOnStatusChanged(webEvent -> {

//Call value change

onValueChange(webEvent.getData());

});

}

});

return browser;

}

First we create the webview and engine, then load the editor.html file into the webview. Next, we fetch the loadWorker, which is what controls the window.status object we are writing to with the JS. We then get the state property, and add an listener. If the listener is added successfully, then listen for a change event.

When the change event, we'll call our onValueChange method:

//called when value changes

private void onValueChange(String data){

//Print out data

System.out.println(data);

//If the data is equal to "exit", close the program

if("exit".equals(data)){

//Print goodbye message

System.out.println("Received exit command! Goodbye :D");

//Exit

System.exit(0);

}

}

This is extremely simple. onValueChange is called with the textarea value as the data parameter. In this case, I'm printing it to the console. If I type exit into the textarea, then it will print a goodbye message and close the application.

yfsKB.png

And that's it! If you run the above code, then it'll log the textarea value into the console real-time!

Note:

Your project structure should look this, with editor in the same dir as the java file, if you want this code to work straight off:

hDwBK.png

Also, I'm obviously missing the imports in the java code.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值