The HTMLEditor control is a rich text editor with the following features.
The HTMLEditor class returns the editing content in the HTML string.
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.web.HTMLEditor; import javafx.stage.Stage; /*from w w w . j a v a2 s . co m*/ public class Main extends Application { @Override public void start(Stage stage) { HTMLEditor htmlEditor = new HTMLEditor(); htmlEditor.setPrefHeight(245); Scene scene = new Scene(htmlEditor); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
The code above generates the following result.
To set content to HTMLEditor class, use the setHtmlText method.
htmlEditor.setHtmlText(INITIAL_TEXT);
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.web.HTMLEditor; import javafx.stage.Stage; //from w w w. j ava2 s .c om public class Main extends Application { @Override public void start(Stage stage) { HTMLEditor htmlEditor = new HTMLEditor(); htmlEditor.setPrefHeight(245); String INITIAL_TEXT = "Lorem ipsum dolor sit " + "amet, consectetur adipiscing elit. Nam tortor felis, pulvinar " + "aliquam sagittis gravida eu dolor. Etiam sit amet ipsum " + "sem."; htmlEditor.setHtmlText(INITIAL_TEXT); Scene scene = new Scene(htmlEditor); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
The code above generates the following result.
We can use the HTML tags in this string to apply specific formatting for the initially rendered content.
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.web.HTMLEditor; import javafx.stage.Stage; /* w w w. j a v a 2 s.c om*/ public class Main extends Application { @Override public void start(Stage stage) { HTMLEditor htmlEditor = new HTMLEditor(); htmlEditor.setPrefHeight(245); String INITIAL_TEXT = "Lorem ipsum dolor sit " + "amet, consectetur adipiscing elit. <i>Nam tortor felis</i>, pulvinar " + "<UL><li>a</li><li>a</li><li>a</li></UL>" + "aliquam sagittis gravida <b>eu dolor</b>. Etiam sit amet ipsum " + "sem."; htmlEditor.setHtmlText(INITIAL_TEXT); Scene scene = new Scene(htmlEditor); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
The code above generates the following result.
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.VBox; import javafx.scene.web.HTMLEditor; import javafx.stage.Stage; //from w ww . ja v a2 s . c om public class Main extends Application { @Override public void start(Stage stage) { HTMLEditor htmlEditor = new HTMLEditor(); htmlEditor.setPrefHeight(245); String INITIAL_TEXT = "Lorem ipsum dolor sit " + "amet, consectetur adipiscing elit. <i>Nam tortor felis</i>, pulvinar " + "<UL><li>a</li><li>a</li><li>a</li></UL>" + "aliquam sagittis gravida <b>eu dolor</b>. Etiam sit amet ipsum " + "sem."; htmlEditor.setHtmlText(INITIAL_TEXT); Button showHTMLButton = new Button("Produce HTML Code"); showHTMLButton.setOnAction((ActionEvent arg0) -> { System.out.println(htmlEditor.getHtmlText()); }); VBox vbox = new VBox(); vbox.getChildren().addAll(htmlEditor,showHTMLButton); Scene scene = new Scene(vbox); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }
The code above generates the following result.