By using
‘setContentDisplay’ method, you can set the position of graphic relative to the
text.
Signature of 'setContentDisplay'
method
public
final void setContentDisplay(ContentDisplay value)
ContentDisplay
is an enum, it represents below constants.
ContentDisplay constant
|
Description
|
TOP
|
Content
will be placed at the top of the Label.
|
RIGHT
|
Content
will be placed at the right of the Label.
|
BOTTOM
|
Content
will be placed at the bottom of the Label.
|
LEFT
|
Content
will be placed at the left of the Label.
|
CENTER
|
Content
will be placed at the center of the Label.
|
GRAPHIC_ONLY
|
Only the
content will be displayed.
|
TEXT_ONLY
|
Only the
label's text will be displayed.
|
Ex
Label
label1 = new Label("label1", new ImageView(image));
label1.setContentDisplay(ContentDisplay.BOTTOM);
package com.sample.demos; import static javafx.geometry.Pos.CENTER; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.ContentDisplay; import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class LabelApp extends Application { private static String imageFilePath = "C:\\Users\\krishna\\Documents\\Study\\javaFX\\search.png"; @Override public void start(Stage primaryStage) throws FileNotFoundException { InputStream is = new FileInputStream(imageFilePath); Image image = new Image(is); Label label1 = new Label("label1", new ImageView(image)); label1.setContentDisplay(ContentDisplay.BOTTOM); Label label2 = new Label("label2", new ImageView(image)); label2.setContentDisplay(ContentDisplay.CENTER); Label label3 = new Label("label3", new ImageView(image)); label3.setContentDisplay(ContentDisplay.GRAPHIC_ONLY); Label label4 = new Label("label4", new ImageView(image)); label4.setContentDisplay(ContentDisplay.LEFT); Label label5 = new Label("label5", new ImageView(image)); label5.setContentDisplay(ContentDisplay.RIGHT); Label label6 = new Label("label6", new ImageView(image)); label6.setContentDisplay(ContentDisplay.TEXT_ONLY); Label label7 = new Label("label7", new ImageView(image)); label7.setContentDisplay(ContentDisplay.TOP); VBox vBox = new VBox(10, label1, label2, label3, label4, label5, label6, label7); vBox.setAlignment(CENTER); Scene scene = new Scene(vBox, 600, 800); /* Set the scene to primaryStage, and call the show method */ primaryStage.setTitle("JavaFX Label Example"); primaryStage.setScene(scene); primaryStage.show(); } }
TestFX.java
package com.sample.demos; import javafx.application.Application; public class TestFX { public static void main(String args[]) { Application.launch(LabelApp.class, args); } }
No comments:
Post a Comment