JavaFX Label
The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on a container to display text on the screen. It is mainly used to give instruction or information to the user. Various constructors in javafx.scene.control package for Label are:
- Label(): This constructor is used to create label without any text.
- Label(String newtext): This constructor is used to create a label with the text entered by the user.
- Label(String newtext, Node graphic): This constructor is used to create a Label with user text and graphics.
Example:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class SimpleLabel extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
Label newlb = new Label(" Label in JavaFX ");
StackPane pane = new StackPane();
Scene scene = new Scene(pane,500,400);
pane.getChildren().addAll(newlb);
primaryStage.setScene(scene);
primaryStage.setTitle("Label Example in JavaFX ");
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
Output:
The java file is created which extends the Application class. Also start method is overridden with a primarystage object as an argument in it. In the start method label is created with text specified in it. Then StackPane container is created. An object of StackPane is passed to Scene constructor as an argument.. Then addAll() method is then added to the container. Using show() method on primaryStage is called to display container. Then in main method launch(args) method is called to launch the application.
Right click on the java file, to run the application. Then select Run As, and choose Java application. It will show one container with the title “Label Example in JavaFX” and Label in the center as “Label in JavaFX”.
Displaying Graphics with Label:
We can also display the image in Label. In JavaFX, we are allowed to display graphics next to the label. Using constructor, we can specify graphics with the label.
Example:
import java.io.FileInputStream;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class GraphicsText extends Application
{
@Override
public void start(Stage primaryStage) throws Exception
{
StackPane pane = new StackPane();
FileInputStream file = new FileInputStream ("home.jpg");
Image newimage = new Image(file);
ImageView imagev1 = new ImageView(newimage);
Label my_label = new Label(" Visit Homepage ",imagev1);
Scene scene = new Scene(pane,600,400);
pane.getChildren().add(my_label);
primaryStage.setScene(scene);
primaryStage.setTitle("Label Class Example with home graphics icon ");
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
Output:
The java file is created which extends the Application class. Also start method is overridden with a primarystage object as an argument in it. The StackPane object is created in start(). Also, the FileInputStream object is created using a constructor and specifying the file path. Image object is created and then it is passed in the ImageView constructor. The label constructor is created by passing text and image arguments to it. The scene object is created then the stage is prepared. The title is set to the primary stage and then using the show() method it is displayed. In main method launch(args) method is called to run the application.
Right click on the java file, to run the application. Select Run As, and choose Java application. It will show one container with the title "Label class Example with home graphics icon” and Label in the center as “ Visit homepage” with a home icon.