HTMLEditor class is a part of JavaFX. HTMLEditor allows the user to edit the existing HTML text and also apply styling to the text. The underlying data model is HTML but it is not visible to the user.
Constructor of the class:
- HTMLEditor(): Creates a new object of HTMLEditor.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getHtmlText() | Returns the HTML content of the editor. |
| print(PrinterJob j) | Prints the content of the editor using the given printer job. |
| setHtmlText(String h) | Sets the HTML text of the editor. |
Below programs illustrate the use of HTMLEditor class:
- Java program to create a HTMLEditor and add to the stage: In this program we will create a HTMLEditor named htmleditor. We will also create a TilePane named tilepane,and then add the htmleditor to the tilepane using the getChildren().add() function. We will create a scene and add tilepane to it. We will add the scene to the stage using the setScene() function and display the stage using the show() function to display the final results.
// Java program to create a html editor// and add to the stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.web.HTMLEditor;ÂÂpublicclassEditor_1extendsApplication {   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{       Â// set title for the stage       Âstage.setTitle("Creating HTMLEditor");       Â// create a tile pane       ÂTilePane tilepane =newTilePane();       Â// HTML editor       ÂHTMLEditor htmleditor =newHTMLEditor();       Â// add html editor       Âtilepane.getChildren().add(htmleditor);       Â// create a scene       ÂScene scene =newScene(tilepane,600,500);       Â// set the scene       Âstage.setScene(scene);       Âstage.show();   Â}   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{       Â// launch the application       Âlaunch(args);   Â}}Output:
- Java program to create a HTMLEditor and set initial HTML text to it and add to the stage: In this program we will create a HTMLEditor named htmleditor. We will set the initial HTML text using setHtmlText() function. We will also create a TilePane named tilepane, we will add the htmleditor to the tilepane using the getChildren().add() function. We will create a scene and add tilepane to it. We will add the scene to the stage using the setScene() function and display the stage using the show() function to display the final results.
// Java program to create a html editorÂ// and set initial HTML text to it andÂ// add to the stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.scene.web.HTMLEditor; ÂÂpublicclassEditor_2extendsApplication { Â   Â// launch the application   Âpublicvoidstart(Stage stage)   Â{ Â       Â// set title for the stage       Âstage.setTitle("creating HTMLEditor"); Â       Â// HTML text       ÂString text ="<html><body><h1>Geeks</h1></body></html>"; Â       Â// create a tile pane       ÂTilePane tilepane =newTilePane(); Â       Â// HTML editor       ÂHTMLEditor htmleditor =newHTMLEditor(); Â       Â// set html text       Âhtmleditor.setHtmlText(text); Â       Â// add html editor       Âtilepane.getChildren().add(htmleditor); Â       Â// create a scene       ÂScene scene =newScene(tilepane,600,500); Â       Â// set the scene       Âstage.setScene(scene); Â       Âstage.show();   Â} Â   Â// Main Method   Âpublicstaticvoidmain(String args[])   Â{ Â       Â// launch the application       Âlaunch(args);   Â}}
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/web/HTMLEditor.html

