B4J Question Focus HTMLeditor

ThRuST

Well-Known Member
Licensed User
Longtime User
I found native JavaFX code how to focus the webview in the HTMLeditor, so I ask anyone of you gurus out there to convert this code to B4J. It would be most useful and great to learn from. How to use it with inline Java code #If JAVA code #End if thanks.


Source webpage can be found >here<

JAVAFX
B4X:
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 view = (WebView) ((GridPane)((HTMLEditorSkin)editor.getSkin()).getChildren().get(0)).getChildren().get(2);

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

}

EDIT: I have updated my post because I turned Erel's code into a library. You can find it in B4j Libraries and Classes.
You can go there directly by clicking >here<
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.Show
   MainForm.RootPane.LoadLayout("1")
   Dim jo As JavaObject = HTMLEditor1
   Dim wv As JavaObject = jo.RunMethodJO("getSkin", Null).RunMethodJO("getChildren", Null) _
       .RunMethodJO("get", Array(0)).RunMethodJO("getChildren", Null).RunMethod("get", Array(2))
   Sleep(500)
   Dim mevent As JavaObject
   Dim meventstatic As JavaObject
   meventstatic.InitializeStatic("javafx.scene.input.MouseEvent")
   mevent.InitializeNewInstance("javafx.scene.input.MouseEvent", _
       Array(meventstatic.GetField("MOUSE_PRESSED"), 100.1, 100.1, 200.1, 200.1, "PRIMARY", 1, False, False, False, False, False, False, False, False, False, False, Null))
   wv.RunMethod("fireEvent", Array(mevent))
   HTMLEditor1.RequestFocus
   mevent.InitializeNewInstance("javafx.scene.input.MouseEvent", _
       Array(meventstatic.GetField("MOUSE_RELEASED"), 100.1, 100.1, 200.1, 200.1, "PRIMARY", 1, False, False, False, False, False, False, False, False, False, False, Null))
   wv.RunMethod("fireEvent", Array(mevent))
End Sub
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Of course :) How do I change the version? even if I will leave it at v1.0 when I make the changes quickly. I can write 'Based on a solution by Erel' ok? Cheers
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
Ok. but I can add it in the info before the sub as well. Do you mind adding code also for setting the cursor to the end of all text so I can add it right away.
I mean how can I update the library version in the library? Many users will find this useful thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You have also posted it here: https://www.b4x.com/android/forum/threads/jhtmleditor-library.88465/#post-559868
It is better not to make duplicate libraries.

As a developer I would prefer to use a sub with that code instead of a library.

Do you mind adding code also for setting the cursor to the end of all text so I can add it right away.
You posted Java code and I've helped you reimplement it with JavaObject. Find a solution that changes the cursor position, maybe with JavaScript and I can help you convert it.
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
ok I will delete the file in the top of this post. About how to position the caret inside the HTMEditor I found a solution here
 
Upvote 0

ThRuST

Well-Known Member
Licensed User
Longtime User
It seems like this solution no longer works.

Did you forget that it no longer works? That's why I posted my question in the first place ;)
I am about to add extra features to the jHTMLeditor lib. It's convenient to use multifunctions inside a library, and one function as freestanding sub. Just my personal cup of tea.
 
Upvote 0
Top