Tech and Media Labs
This site uses cookies to improve the user experience.


JavaFX HTMLEditor

Jakob Jenkov
Last update: 2019-08-03

The JavaFX HTMLEditor is an advanced HTML editor that enables the user to edit HTML easier than by writing the full HTML markup in text. The HTMLEditor contains a set of buttons that can be used to set the styles of the edited text WYSIWYG style. The JavaFX HTMLEditor is represented by the class javafx.scene.web.HTMLEditor. Here is a screenshot of a JavaFX HTMLEditor:

A JavaFX HTMLEditor screenshot.

Full HTMLEditor Example

Here is first a full JavaFX HTMLEditor example so you can see what using the HTMLEditor looks like in code:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;

public class HtmlEditorExample extends Application {

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

    public void start(Stage primaryStage) {

        HTMLEditor htmlEditor = new HTMLEditor();

        VBox vBox = new VBox(htmlEditor);
        Scene scene = new Scene(vBox);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX App");

        primaryStage.show();
    }
}

Create an HTMLEditor

Before you can use a JavaFX HTMLEditor in your code, you must first create an instance of it. Here is an example of creating an instance of a JavaFX HTMLEditor:

 HTMLEditor htmlEditor = new HTMLEditor();

Get HTML From HTMLEditor

Sooner or later you will probably want to obtain the HTML text that was edited in the HTMLEditor by the user. You obtain the HTML from the HTMLEditor via its getHtmlText() method. Here is an example of getting the HTML from a JavaFX HTMLEditor instance:

String htmlText = htmlEditor.getHtmlText();

As you can see, the HTML is returned as a standard Java String.

Set HTML in HTMLEditor

You can also set the HTML to be edited in a JavaFX HTMLEditor via its setHtmlText() method. Here is an example of setting the HTML to be edited in a JavaFX HTMLEditor instance:

String htmlText = "<b>Bold text</b>";

htmlEditor.setHtmlText(htmlText);

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC