问题描述
是否有可能在JavaFX中的WebView中获取HTML输入的值?
如果是这样,我将如何使用事件以便自动更新值?
示例应用程序:
package webviewinputvalue;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
导入javafx.scene.web.WebView;
import javafx.stage.Stage;
public class WebviewInputValue extends Application {
$ b $ @Override
public void start(Stage primaryStage){
WebView test = new WebView( );
WebEngine run = test.getEngine();
run.loadContent(< textarea>< / textarea>);
StackPane root = new StackPane();
root.getChildren()。add(test);
场景场景=新场景(root,300,250);
primaryStage.setTitle(Hello World!);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String [] args){
launch(args);
}
}
创建一个这样的简单窗口:
问题:是否可以使用JavaFX获取textarea的值?如果是这样,我应该怎么做?
解决方案但它需要一些简单的解决方法。
首先,我将把HTML移动到它自己的单独文件中,以便编辑更简单,通常会创建更清晰的代码。
editor.html
< !DOCTYPE html>
< html>
< head>
< meta charset =UTF-8>
< title>示例Live-view< / title>
< meta name =viewportcontent =width = device-width,initial-scale = 1.0>
< / head>
< body>
< script>
document.querySelector(textarea)。addEventListener(keyup,function(){
window.status = this.value;
});
< / script>
< / body>
< / html>
基本结构非常简单,但< script>
元素?它是什么?
document.querySelector(textarea)。addEventListener(keyup,function(){
window.status = this.value;
});
AFAIK,不可能直接在JavaFX中获取输入的值,所以您必须做出解决方法。在这种情况下,只要textarea中存在 keyup
事件,我就用textarea值设置窗口状态。
现在我们需要在我们的JavaFX中设置webview。
WebviewInputValue.java
package webviewinputvalue;
public class WebviewInputValue extends Application {
private WebView InitWebview(){
//创建浏览器
WebView浏览器=新的WebView( );
WebEngine render = browser.getEngine();
//载入简单HTML
字符串编辑器= WebviewInputValue.class.getResource(editor.html)。toExternalForm();
render.load(editor);
//监听状态变化
render.getLoadWorker()。stateProperty()。addListener((ov,o,n) - > {
if(Worker.State .SUCCEEDED == n){
render.setOnStatusChanged(webEvent - > {
//调用值更改
onValueChange(webEvent.getData());
});
}
});
返回浏览器;
}
//值改变时调用
private void onValueChange(String data){
//输出数据
系统。通过out.println(数据);
//如果数据等于exit,则关闭程序
if(exit.equals(data)){
// Print再见消息
System.out.println(Received exit command!Goodbye:D);
//退出
System.exit(0);
@Override
public void start(Stage primaryStage){
//创建浏览器
WebView浏览器= InitWebview();
StackPane root = new StackPane();
root.getChildren()。add(browser);
场景场景=新场景(root,300,250);
primaryStage.setTitle(Hello World!);
primaryStage.setScene(scene);
primaryStage.show();
$ ** b $ b * @param args命令行参数
* /
public static void main(String [] args){
发射(参数);
}
}
再次,非常简单的代码。让我们深入研究它:
private WebView InitWebview(){
//创建浏览器
WebView浏览器=新的WebView();
WebEngine render = browser.getEngine();
//载入简单HTML
字符串编辑器= WebviewInputValue.class.getResource(editor.html)。toExternalForm();
render.load(editor);
//监听状态变化
render.getLoadWorker()。stateProperty()。addListener((ov,o,n) - > {
if(Worker.State .SUCCEEDED == n){
render.setOnStatusChanged(webEvent - > {
//调用值更改
onValueChange(webEvent.getData());
});
}
});
返回浏览器;
}
首先我们创建webview和引擎,然后加载 editor.html
文件放到webview中。接下来,我们获取loadWorker,它负责控制我们写给JS的 window.status
对象。然后我们得到状态
属性,并添加一个监听器。如果侦听器已成功添加,请监听更改事件。
当更改事件时,我们将调用我们的 onValueChange
方法:
//当值更改时调用
private void onValueChange(String data){
//输出数据
的System.out.println(数据);
//如果数据等于exit,则关闭程序
if(exit.equals(data)){
// Print再见消息
System.out.println(Received exit command!Goodbye:D);
//退出
System.exit(0);
}
}
这非常简单。使用 textarea
值调用 onValueChange
作为 data
参数。在这种情况下,我将它打印到控制台。如果我在中输入 exit
,它会打印一条再见消息并关闭应用程序。
就是这样!如果你运行上面的代码,那么它会实时将textarea的值记录到控制台中!
注意:
如果你想让这段代码直接工作,你的项目结构应该看起来像这样,编辑器和java文件在同一个目录下:
另外,我明显错过了java代码中的导入。
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("<textarea></textarea>");
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:
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Example Live-view</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<textarea placeholder="Typing 'exit' will kill the program."></textarea>
<script>
document.querySelector("textarea").addEventListener("keyup", function(){
window.status = this.value;
});
</script>
</body>
</html>
The basic structure is really simple, but what about the <script>
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.
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:
Also, I'm obviously missing the imports in the java code.
这篇关于在JavaFX WebView中获取HTML输入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!