htmljavafxcolor-pickerhtml-editor

How do i change color HTMLeditor with code?


I made a custom htmleditor that extends HTMLEditor. I removed some buttons and added some.

Now i want to add a couple buttons that change the text typed to 1 specific color. But i cant find a way to change the text color in the setOnAction of the button. I tryed a couple ways.

1 : html that i insert at the cursor position

2 : setting the colorpicker's of the htmleditor's color

(jsCodeInsertHtml string allows html at cursor)

    String jsCodeInsertHtml = "function insertHtmlAtCursor(html) {\n" +
            "    var range, node;\n" +
            "    if (window.getSelection && window.getSelection().getRangeAt) {\n" +
            "        range = window.getSelection().getRangeAt(0);\n" +
            "        node = range.createContextualFragment(html);\n" +
            "        range.insertNode(node);\n" +
            "    } else if (document.selection && document.selection.createRange) {\n" +
            "        document.selection.createRange().pasteHTML(html);\n" +
            "    }\n" +
            "}insertHtmlAtCursor('####html####')";



          // add a custom button to the top toolbar.
            Node nodetop = editor.lookup(".top-toolbar");
            if (nodetop instanceof ToolBar) {
                ToolBar topbar = (ToolBar) nodetop;



                //var1 color
                ImageView graphic = new ImageView(var1pic);
                Button colorVar1Button = new Button("", graphic);
                colorVar1Button.setFocusTraversable(false);
                colorVar1Button.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent arg0) {
                        WebView webView=(WebView)editor.lookup("WebView");
                        WebEngine engine= webView.getEngine();

                        //try 1
                        engine.executeScript(jsCodeInsertHtml.replace("####html####","<p><span style=\"color: rgb(200, 200, 200); caret-color: rgb(200, 200, 200);\">"));
                         //the HTML is inserted , but no colorchange. i tryed some htmlvariants aswel


                        //try 2
                        Node color = editor.lookup(".html-editor-foreground");
                        ((ColorPicker)color).setValue(Color.rgb(200,200,200));
                        // i notice the button change in the colorpicker ,
                        // but no change in color happens

                topbar.getItems().addAll(colorVar1Button);

Solution

  • Found the solution. i just had to fire the actionevent of the colorpicker to make it work

                    colorVar1Button.setOnAction(new EventHandler<ActionEvent>() {
                        @Override
                        public void handle(ActionEvent arg0) {
                            Node color = editor.lookup(".html-editor-foreground");
    
                            ((ColorPicker)color).setValue(Color.HOTPINK);
                            color.fireEvent(new ActionEvent());
                        }
                    });
    
                topbar.getItems().addAll(colorVar1Button);