Trying to do a requestFocus() on the WebView does not work until the user has first clicked on the control.
I know this must be possible as htmlEditor can be focused this way (and I suspect it is based on a contenteditable WebView).
I am coding my own specialized htmlEditor using a webview with "contenteditable" and I would really like to be able to focus it like I can do with the standard htmlEditor.
Example code to reproduce the issue:
public class WebViewConteneditableDemo extends Application {
/**
* Initial editor content
*/
String initialEditview = "<html><head>"
+ "</head><body bgcolor = 'lightgrey' contenteditable='true'>"
+"</body></html>";
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Webview focus demo");
final WebView editor = new WebView();
Button btn = new Button();
btn.setText("Test Webview focus");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
editor.requestFocus();
}
});
BorderPane root = new BorderPane();
root.setTop(btn);
root.setCenter(editor);
editor.getEngine().loadContent(initialEditview);
primaryStage.setScene(new Scene(root, 500, 450));
primaryStage.show();
}
}
I know this must be possible as htmlEditor can be focused this way (and I suspect it is based on a contenteditable WebView).
I am coding my own specialized htmlEditor using a webview with "contenteditable" and I would really like to be able to focus it like I can do with the standard htmlEditor.
Example code to reproduce the issue:
public class WebViewConteneditableDemo extends Application {
/**
* Initial editor content
*/
String initialEditview = "<html><head>"
+ "</head><body bgcolor = 'lightgrey' contenteditable='true'>"
+"</body></html>";
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Webview focus demo");
final WebView editor = new WebView();
Button btn = new Button();
btn.setText("Test Webview focus");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
editor.requestFocus();
}
});
BorderPane root = new BorderPane();
root.setTop(btn);
root.setCenter(editor);
editor.getEngine().loadContent(initialEditview);
primaryStage.setScene(new Scene(root, 500, 450));
primaryStage.show();
}
}