The Label class in the javafx.scene.control package of the JavaFX API displays a text element.
We can wrap a text element to fit the specific space, add a graphical image, or apply visual effects by using JavaFX Label control.
The following code shows how to use Label to display Text.
import javafx.application.Application; import javafx.geometry.HPos; import javafx.geometry.Insets; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.GridPane; import javafx.scene.paint.Color; import javafx.stage.Stage; public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { Group root = new Group(); Scene scene = new Scene(root, 300, 130, Color.WHITE); GridPane gridpane = new GridPane(); gridpane.setPadding(new Insets(5)); gridpane.setHgap(10); gridpane.setVgap(10); Label label = new Label("Label"); GridPane.setHalignment(label, HPos.CENTER); gridpane.add(label, 0, 0); root.getChildren().add(gridpane); primaryStage.setScene(scene); primaryStage.show(); } }
The JavaFX API provides three constructors of the Label class for creating labels.
//An empty label Label label1 = new Label(); //A label with the text element Label label2 = new Label("Name"); //A label with the text element and graphical icon Image image = new Image(getClass().getResourceAsStream("labels.jpg")); Label label3 = new Label("Name", new ImageView(image));
After creating a label, we can add textual and graphical content by using the following methods from the Label class.
setText(String text)
- set the text caption for the labelsetGraphic(Node graphic)
- set the graphical icon
setGraphicTextGap
method sets the gap between text and the icon.
The setTextFill method sets the color for the text of the label.
The following code creates a text label, adds an icon to it, and sets a fill color for the text.
Label label1 = new Label("Name"); Image image = new Image(getClass().getResourceAsStream("icon.jpg")); label1.setGraphic(new ImageView(image)); label1.setTextFill(Color.web("#FF76a3"));
The following code shows how to set Label Text color.
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.paint.Color; import javafx.stage.Stage; public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { Scene scene = new Scene(new Group()); stage.setTitle("Label Sample"); stage.setWidth(400); stage.setHeight(180); HBox hbox = new HBox(); Label label1 = new Label("Search"); label1.setTextFill(Color.web("#0076a3")); hbox.setSpacing(10); hbox.getChildren().add((label1)); ((Group) scene.getRoot()).getChildren().add(hbox); stage.setScene(scene); stage.show(); } }
setTextAlignment method can set the alignment of the label content within its layout area.
setContentDisplay method sets the position of the graphic relative to the text. The method acceptsone of the following ContentDisplay constant: LFFT, RIGHT, CENTER, TOP, BOTTOM.
If not set a font for the Label control it is rendered with the default font size.
To set a font text size, use the setFont method from the Label class.
The following code sets the size of the label1 text to 30 points and the font name to Arial.
label.setFont(new Font("Arial", 30));
Sets the text size to 32 points and the font name to Cambria.
label.setFont(Font.font("Cambria", 32));
The following code shows how to set Font for Label.
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.text.Font; import javafx.stage.Stage; public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { Scene scene = new Scene(new Group()); stage.setWidth(400); stage.setHeight(180); HBox hbox = new HBox(); Label label1 = new Label("Search"); label1.setFont(new Font("Arial", 30)); hbox.setSpacing(10); hbox.getChildren().add((label1)); ((Group) scene.getRoot()).getChildren().add(hbox); stage.setScene(scene); stage.show(); } }
To wrap the text to fit text into the layout area, set the true value for the setWrapText method.
Label label = new Label("A long long long long long text"); label.setWrapText(true);
The following code shows how to wrap a Label.
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.text.TextAlignment; import javafx.stage.Stage; //from www .j a v a2 s .co m public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { Scene scene = new Scene(new Group()); stage.setTitle("Label Sample"); stage.setWidth(400); stage.setHeight(180); HBox hbox = new HBox(); Label label1 = new Label("Search long long long long long long long long long "); label1.setPrefWidth(100); label1.setWrapText(true); hbox.setSpacing(10); hbox.getChildren().add((label1)); ((Group) scene.getRoot()).getChildren().add(hbox); stage.setScene(scene); stage.show(); } }
We can control how to render text from a label using the setTextOverrun method when it is impossible to render the text string.
setTextOverrun method accepts one of the OverrunStyle values.
The code above generates the following result.
We can apply visual effects or transformations to a Label control.
The following code rotates a label 270 degrees and translates its position vertically.
Label label = new Label("Name"); label.setRotate(270); label.setTranslateY(50);
The following code shows how to use rotate to create vertical label.
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.text.TextAlignment; import javafx.stage.Stage; public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { Scene scene = new Scene(new Group()); stage.setTitle("Label Sample"); stage.setWidth(400); stage.setHeight(180); HBox hbox = new HBox(); Label label1 = new Label("Search"); label1.setRotate(270); hbox.setSpacing(10); hbox.getChildren().add((label1)); ((Group) scene.getRoot()).getChildren().add(hbox); stage.setScene(scene); stage.show(); } }
The following code shows how to move a Label by using setTranslateY.
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.text.TextAlignment; import javafx.stage.Stage; /*from w w w . j a v a 2s.c o m*/ public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { Scene scene = new Scene(new Group()); stage.setTitle("Label Sample"); stage.setWidth(400); stage.setHeight(180); HBox hbox = new HBox(); Label label1 = new Label("Search"); label1.setTranslateY(50); hbox.setSpacing(10); hbox.getChildren().add((label1)); ((Group) scene.getRoot()).getChildren().add(hbox); stage.setScene(scene); stage.show(); } }
We can zoom a label when a user hovers the mouse cursor over it.
The following code applies the zoom effect to a label when the MOUSE_ENTERED event is fired on the label.
The following code shows how to scale a Label.
import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.input.MouseEvent; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { Scene scene = new Scene(new Group()); stage.setTitle("Label Sample"); stage.setWidth(400); stage.setHeight(180); HBox hbox = new HBox(); final Label label1 = new Label("Search long long long long long long long long long "); label1.setOnMouseEntered(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent e) { label1.setScaleX(1.5); label1.setScaleY(1.5); } }); label1.setOnMouseExited(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent e) { label1.setScaleX(1); label1.setScaleY(1); } }); hbox.setSpacing(10); hbox.getChildren().add((label1)); ((Group) scene.getRoot()).getChildren().add(hbox); stage.setScene(scene); stage.show(); } }
The code above generates the following result.
The following code shows how to add mouse in and out event handler for label.
import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.input.MouseEvent; import javafx.scene.layout.HBox; import javafx.stage.Stage; // w w w. j a v a2 s. co m public class Main extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { Scene scene = new Scene(new Group()); stage.setTitle("Label Sample"); stage.setWidth(400); stage.setHeight(180); HBox hbox = new HBox(); final Label label1 = new Label("Search long long long long long long long long long "); label1.setOnMouseEntered(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent e) { label1.setScaleX(1.5); label1.setScaleY(1.5); } }); label1.setOnMouseExited(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent e) { label1.setScaleX(1); label1.setScaleY(1); } }); hbox.setSpacing(10); hbox.getChildren().add((label1)); ((Group) scene.getRoot()).getChildren().add(hbox); stage.setScene(scene); stage.show(); } }
The code above generates the following result.
The following code shows how to change Label text in Button click event.
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.stage.Stage; /*from w w w. j a v a2 s . c o m*/ public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { Button btn = new Button(); final Label lbl = new Label(); primaryStage.setTitle("Hello World!"); lbl.setLayoutX(70); lbl.setLayoutY(150); btn.setLayoutX(100); btn.setLayoutY(100); btn.setText("Hello, World!"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { lbl.setText("Hello, World."); } }); Group root = new Group(); root.getChildren().add(btn); root.getChildren().add(lbl); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); } }
The code above generates the following result.