We’ve collected these simple code snippets to allow you learn JavaFX.
Example 1: Htmleditor
Study the following code:
Step 1: Create Project
- Open your favorite Java IDE.
- In the menu go to
File --> Create New Project
.
Step 2: Dependencies
No dependencies are needed for this project.
Step 3: Write Code
Our code will comprise the following java files:
HtmlEditorExample.java
- In your editor or IDE, create a file known as
HtmlEditorExample.java
. - Then add the following code:
(a). HtmlEditorExample.java
We will start by adding some imports to this class:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;
Through inheritance we will be able to derive properties from a parent class
. However we have to extend that parent class
. So we do that using the extends
keyword.
public class HtmlEditorExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
All Java applications have an entry point known as the main
method. We will define it:
public static void main(String[] args) {
Here is the full code:
package com.jenkov.javafx.htmleditor;
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();
String htmlText = "<b>Bold text</b>";
htmlEditor.setHtmlText(htmlText);
String htmlTextEdited = htmlEditor.getHtmlText();
System.out.println(htmlTextEdited);
VBox vBox = new VBox(htmlEditor);
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX App");
primaryStage.show();
}
}
Download
Download the code using the below links:
Number | Link |
---|---|
1. | Download Example |
2. | Follow code author |
3. | Code: Apache 2.0 License |