Label widget is used to define non-editable text control.
Can a label has event handlers?
No
Label class provides following constructors to get an instance.
public Label()
public Label(String text)
public Label(String text, Node graphic)
'text' specifies the text to be displayed in the label.
How to set a font to the label?
label1.setFont(Font.font("Verdana", FontPosture.REGULAR, 45));
How to set alignment?
label3.setAlignment(Pos.TOP_LEFT);
How to add image to label?
Using ‘setGraphic’ method, you can set graphic icon to the label.
String filename = "javaIcon.png";
InputStream in = this.getClass().getClassLoader().getResourceAsStream(filename);
Image image = new Image(in);
ImageView imageView = new ImageView(image);
label7.setGraphic(imageView);
Find the below working application.
labelDemo.fxml
<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.control.*?>
<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.widgets.controller.LabelController"
fx:id="vBox1" spacing="20" alignment="center">
<Label fx:id="label1" text="Simple Text"/>
<Label fx:id="label2" text="Simple Italic Text"/>
<Label fx:id="label3" text="Top left aligned Text"/>
<Label fx:id="label4" text="Blue colored text" textFill="blue"/>
<Label fx:id="label5" text="Text underlined" textFill="red" underline="true"/>
<Label fx:id="label6" text="This text will be wrapped in multiple lines" textFill="red"
underline="true"
wrapText="true"/>
<Label fx:id="label7" text="Java Icon" textFill="green" wrapText="true"/>
</VBox>
LabelController.java
package com.sample.app.widgets.controller;
import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
public class LabelController implements Initializable {
@FXML
private Label label1;
@FXML
private Label label2;
@FXML
private Label label3;
@FXML
private Label label4;
@FXML
private Label label5;
@FXML
private Label label6;
@FXML
private Label label7;
@FXML
private VBox vBox1;
@Override
public void initialize(URL location, ResourceBundle resources) {
DropShadow dropShadow = new DropShadow();
dropShadow.setOffsetX(5);
dropShadow.setOffsetY(5);
dropShadow.setColor(Color.GRAY);
vBox1.setEffect(dropShadow);
label1.setFont(Font.font("Verdana", FontPosture.REGULAR, 45));
label2.setFont(Font.font("Verdana", FontPosture.ITALIC, 45));
label3.setFont(Font.font("Verdana", FontPosture.REGULAR, 45));
label3.setAlignment(Pos.TOP_LEFT);
label4.setFont(Font.font("Verdana", FontPosture.REGULAR, 45));
label5.setFont(Font.font("Verdana", FontPosture.REGULAR, 45));
label6.setFont(Font.font("Verdana", FontPosture.REGULAR, 45));
label7.setFont(Font.font("Verdana", FontPosture.REGULAR, 45));
String filename = "javaIcon.png";
InputStream in = this.getClass().getClassLoader().getResourceAsStream(filename);
Image image = new Image(in);
ImageView imageView = new ImageView(image);
label7.setGraphic(imageView);
label7.setContentDisplay(ContentDisplay.RIGHT);
}
}
LabelDemo.java
package com.sample.app.widgets;
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 LabelDemo extends Application {
private static final String FXML_FILE = "/labelDemo.fxml";
private static final String STAGE_TITLE = "Label Demo Effect";
public static void main(String args[]) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = (Parent) FXMLLoader.load(this.getClass().getResource(FXML_FILE));
Scene scene = new Scene(root, 900, 900, Color.WHITE);
primaryStage.setTitle(STAGE_TITLE);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Output
No comments:
Post a Comment