Webview with contenteditable cannot be focused programmatically

前端 未结 2 2044
傲寒
傲寒 2020-12-10 22:59

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

2条回答
  •  Happy的楠姐
    2020-12-10 23:54

    you can focus the webview of the HTMLEditor this way:

    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));
            });
        }
    
    }
    

提交回复
热议问题