2

在用户第一次单击控件之前,尝试在 WebView 上执行 requestFocus() 不起作用。

我知道这一定是可能的,因为 htmlEditor 可以以这种方式集中注意力(我怀疑它基于 contenteditable WebView)。

我正在使用带有“contenteditable”的 webview 编写我自己的专用 htmlEditor,我真的希望能够像使用标准 htmlEditor 一样聚焦它。

我相信这一定是 Javafx 的问题,我已经将它提交给了 Jira,但我想知道是否有人能想到一个解决方法。

更新:jira 中的问题编号:RT-21695

简短的演示代码:

/* Demo webview */

public class WebViewConteneditableDemo extends Application {


String initialEditview = "<html><head>"
        + "</head><body 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();
}

}

4

2 回答 2

4

requestFocus api 只是一个焦点请求,它不保证焦点。

有时,其他 JavaFX 控件的内部实现在您请求焦点之前或之后请求焦点,最终在您的 requestFocus 调用中没有任何效果。

通常,您可以通过将 requestFocus 调用包装在Platform.runLater中或使用带有KeyFrame的Timeline来使 requestFocus 调用生效,该 KeyFrame在延迟后调用 requestFocus。

如果这些都不起作用,那么 JavaFX 团队可以在您提交的 jira 上下文中解决 WebView 的 requestFocus 处理中可能存在错误。

更新

问题中的示例代码中的具体问题是,虽然 WebView 获得了焦点,但 WebView 中的可编辑内容元素没有获得焦点。

我尝试仅将示例代码中的 html 加载<html><head></head><body contenteditable='true'></body></html>到 firefox 中,它的行为与 JavaFX WebView 完全相同(即,可编辑的内容元素在被点击之前没有被聚焦)。所以我不认为这是 WebView 的问题。

要获得可编辑元素的焦点,您需要在希望它集中时执行一些脚本,例如在 javascript onload 钩子中<body onLoad='document.body.focus();' contenteditable='true'/>

这是一个可执行的示例应用程序,它演示了对 JavaFX WebView 中 contenteditable 元素的焦点行为的编程控制:

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 = "<body bgcolor='cornsilk' onLoad='document.body.focus();' contenteditable='true'/>";
  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<ActionEvent>() {
      @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();
  }
}
于 2012-05-21T16:44:00.493 回答
3

您可以通过这种方式聚焦 HTMLEditor 的 webview:

import com.sun.javafx.scene.web.skin.HTMLEditorSkin;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.web.HTMLEditor;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class FocusTest extends Application {

    public static void main(final String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        final HTMLEditor editor = new HTMLEditor();
        final WebView editorView = (WebView) editor.lookup(".web-view");

        primaryStage.setScene(new Scene(editor));
        primaryStage.sizeToScene();
        primaryStage.show();

        Platform.runLater(() -> {
            view.fireEvent(new MouseEvent(MouseEvent.MOUSE_PRESSED, 100, 100, 200, 200, MouseButton.PRIMARY, 1, false, false, false, false, false, false, false, false, false, false, null));
            editor.requestFocus();
            view.fireEvent(new MouseEvent(MouseEvent.MOUSE_RELEASED, 100, 100, 200, 200, MouseButton.PRIMARY, 1, false, false, false, false, false, false, false, false, false, false, null));
        });
    }

}
于 2015-07-21T22:20:01.833 回答