JavaFX | HTMLEditor Class

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:

  1. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to create a html editor
    // and add to the stage
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.web.HTMLEditor;
      
    public class Editor_1 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            // set title for the stage
            stage.setTitle("Creating HTMLEditor");
      
            // create a tile pane
            TilePane tilepane = new TilePane();
      
            // HTML editor
            HTMLEditor htmleditor = new HTMLEditor();
      
            // add html editor
            tilepane.getChildren().add(htmleditor);
      
            // create a scene
            Scene scene = new Scene(tilepane, 600, 500);
      
            // set the scene
            stage.setScene(scene);
      
            stage.show();
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }

    chevron_right

    
    

    Output:


  2. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to create a html editor 
    // and set initial HTML text to it and 
    // add to the stage
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.web.HTMLEditor;
       
    public class Editor_2 extends Application {
       
        // launch the application
        public void start(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 = new TilePane();
       
            // HTML editor
            HTMLEditor htmleditor = new HTMLEditor();
       
            // set html text
            htmleditor.setHtmlText(text);
       
            // add html editor
            tilepane.getChildren().add(htmleditor);
       
            // create a scene
            Scene scene = new Scene(tilepane, 600, 500);
       
            // set the scene
            stage.setScene(scene);
       
            stage.show();
        }
       
        // Main Method
        public static void main(String args[])
        {
       
            // launch the application
            launch(args);
        }
    }

    chevron_right

    
    

    Output:


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



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.