Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#requestFocus%28%29" rel="nofollow">requestFocus</a> api is just a request for focus, it does not guarantee focus.</p> <p>Sometimes the internal implementation of other JavaFX controls request focus before or after you have requested focus which ends up in your requestFocus call not having any effect.</p> <p>Often you can make the requestFocus call take effect by either wrapping it in a <a href="http://docs.oracle.com/javafx/2/api/javafx/application/Platform.html#runLater%28java.lang.Runnable%29" rel="nofollow">Platform.runLater</a> or using a <a href="http://docs.oracle.com/javafx/2/api/javafx/animation/Timeline.html" rel="nofollow">Timeline</a> with a <a href="http://docs.oracle.com/javafx/2/api/javafx/animation/KeyFrame.html" rel="nofollow">KeyFrame</a> which invokes requestFocus after a delay.</p> <p>If neither of those work, then there is likely a bug in the requestFocus processing for WebView which the JavaFX team can address in the context of the jira you filed.</p> <p><strong>Update</strong></p> <p>The specific issue in the sample code in the question was that, although the WebView was focused, the editable content element in the WebView was not focused.</p> <p>I tried loading just the html from the sample code <code>&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body contenteditable='true'&gt;&lt;/body&gt;&lt;/html&gt;</code> into firefox and it behaved exactly the same as the JavaFX WebView (i.e. the editable content element was not focused until it was clicked on). So I don't believe this is an issue with WebView.</p> <p>To get the editable element focused, you need to execute some script when you want it focused, for example in the javascript onload hook <code>&lt;body onLoad='document.body.focus();' contenteditable='true'/&gt;</code></p> <p>Here is an executable sample application which demonstrates programmatic control of focus behaviour of contenteditable elements in a JavaFX WebView:</p> <pre><code>import javafx.application.Application; import javafx.beans.binding.Bindings; import javafx.event.*; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.scene.web.WebView; import javafx.stage.Stage; public class WebViewEditable extends Application { String content = "&lt;body bgcolor='cornsilk' onLoad='document.body.focus();' contenteditable='true'/&gt;"; public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { final WebView editor = new WebView(); editor.getEngine().loadContent(content); Button webviewFocusButton = new Button("Focus on WebView"); webviewFocusButton.setOnAction(new EventHandler&lt;ActionEvent&gt;() { @Override public void handle(ActionEvent event) { editor.getEngine().executeScript("document.body.focus()"); editor.requestFocus(); } }); Button selfFocusButton = new Button("Focus on this Button"); Label focusLabel = new Label(); focusLabel.textProperty().bind(Bindings .when(editor.focusedProperty()) .then("WebView has the focus.") .otherwise("WebView does not have the focus.") ); focusLabel.setMaxWidth(Double.MAX_VALUE); focusLabel.setStyle("-fx-background-color: coral; -fx-padding: 5;"); BorderPane layout = new BorderPane(); layout.setTop(HBoxBuilder.create().spacing(10).children(webviewFocusButton, selfFocusButton).style("-fx-padding: 10; -fx-background-color: palegreen").build()); layout.setCenter(editor); layout.setBottom(focusLabel); stage.setScene(new Scene(layout)); stage.show(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload