Text widget is used to define a node that displays text.
How to define Text widget?
Text class provides below constructors to define a Text widget.
public Text()
public Text(String text)
public Text(double x, double y, String text)
‘x’ specifies horizontal position of the text.
‘y’ specifies vertical position of the text.
How to define a text element in fxml?
You can define using Text element.
Example
<Text fx:id = "text" text="Learning JavaFX" fill="lightgreen"
style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
stroke="darkblue" strokeWidth="2"
underline="true"/>
Find the below working application.
textDemo.fxml
<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.controller.TextController">
<Text fx:id = "text" text="Learning JavaFX" fill="lightgreen"
style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
stroke="darkblue" strokeWidth="2"
underline="true"/>
</StackPane>
TextController.java
package com.sample.app.controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.text.Text;
public class TextController implements Initializable {
@FXML
private Text text;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
}
TextControllerDemo.java
package com.sample.app;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class TextControllerDemo extends Application {
public static void main(String args[]) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = (Parent) FXMLLoader.load(TextControllerDemo.class.getResource("/textDemo.fxml"));
Scene scene = new Scene(root, 600, 300, Color.WHITE);
primaryStage.setTitle("Text Controller Demo");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Output
Previous Next Home
No comments:
Post a Comment