Reputation: 805
In JavaFX, when I create an HTMLEditor, it appears at first with the buttons greyed out, like so
Only after I click in the editing area does the editor come to life (buttons no longer disabled) and the cursor appears
I want the editor to load without becoming disabled, and be ready for editing right away without having to click on the edit area first. I.e. cursor ready to enter text, buttons not disabled.
I've tried some different things:
htmlEditor.requestFocus();
does nothing. Also I tried simulating a click in the WebEngine, which also had no effect:
// for manipulating the html structure of the editor
WebView webView;
WebEngine webEngine;
...
StringBuilder script = new StringBuilder();
script.append("function eventFire(el, etype){");
script.append("if (el.fireEvent) {");
script.append("el.fireEvent('on' + etype);");
script.append("} else {");
script.append("var evObj = document.createEvent('Events');");
script.append("evObj.initEvent(etype, true, false);");
script.append("el.dispatchEvent(evObj);");
script.append("}");
script.append("}");
script.append("eventFire(document.getElementsByTagName('html')[0], 'click');");
webEngine.executeScript(script.toString());
Is there a way to do it? Thanks for your help.
Upvotes: 2
Views: 225
Reputation: 805
As suggested here, this seems to do the trick:
Platform.runLater( () ->
{
webView.fireEvent( new MouseEvent( MouseEvent.MOUSE_PRESSED, 100, 100, 200, 200, MouseButton.PRIMARY, 1, false, false, false, false,
false, false, false, false, false, false, null ) );
htmlEditor.requestFocus();
webView.fireEvent( new MouseEvent( MouseEvent.MOUSE_RELEASED, 100, 100, 200, 200, MouseButton.PRIMARY, 1, false, false, false, false,
false, false, false, false, false, false, null ) );
} );
I had been working with this idea for a while but it never worked because I didnt notice I had actually imported MouseEvent from the wrong package so wasn't getting the functionality i expected. Thanks for your help.
Upvotes: 2