Welcome guys. Learn the JavaFX UI widgets using these code snippets.
Example 1: Text
Let us look at the 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:
TextExample.java
- In your editor or IDE, create a file known as
TextExample.java
. - Then add the following code:
(a). TextExample.java
Like in every program we write, we will need to import functionalities into our project. Add the following imports:
import javafx.application.Application;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontSmoothingType;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
Extend the class
as shown below:
public class TextExample extends Application {
Our class
will have the following methods:
void main(String[] args)
void start(Stage primaryStage)
The starting point of our Java app will be main method which we create as follows:
public static void main(String[] args) {
In this particular class
we will be overriding our void start(Stage primaryStage)
method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:
@Override
public void start(Stage primaryStage) {
Text text = new Text("This is a JavaFX text. Line 2");
//text.setWrappingWidth(80);
//text.setStrikethrough(true);
//text.setUnderline(true);
text.setFontSmoothingType(FontSmoothingType.GRAY);
text.setFontSmoothingType(FontSmoothingType.LCD);
Text text2 = new Text();
text2.setText("123");
text2.setFont(Font.font("Arial"));
text2.setFont(Font.font("Arial", FontWeight.BOLD, 36));
text.setFill(Color.DARKMAGENTA);
//text.setStroke(Color.GREEN);
text.setX(0);
text.setY(20);
text.setTextOrigin(VPos.BASELINE);
//Scene scene = new Scene(new VBox(text), 300, 250);
Scene scene = new Scene(new Pane(text), 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
Here is the full code:
package com.jenkov.javafx.text;
import javafx.application.Application;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontSmoothingType;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class TextExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
Text text = new Text("This is a JavaFX text. Line 2");
//text.setWrappingWidth(80);
//text.setStrikethrough(true);
//text.setUnderline(true);
text.setFontSmoothingType(FontSmoothingType.GRAY);
text.setFontSmoothingType(FontSmoothingType.LCD);
Text text2 = new Text();
text2.setText("123");
text2.setFont(Font.font("Arial"));
text2.setFont(Font.font("Arial", FontWeight.BOLD, 36));
text.setFill(Color.DARKMAGENTA);
//text.setStroke(Color.GREEN);
text.setX(0);
text.setY(20);
text.setTextOrigin(VPos.BASELINE);
//Scene scene = new Scene(new VBox(text), 300, 250);
Scene scene = new Scene(new Pane(text), 300, 250);
primaryStage.setScene(scene);
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 |