Wednesday, 9 May 2018

JavaFX: Label: Set gap between text and graphic image

You can use 'setGraphicTextGap' method to set the gap between the label text and graphical icon associated with it.

Ex
InputStream is = new FileInputStream(imageFilePath);
Image image = new Image(is);
Label label = new Label("Java Icon", new ImageView(image));
label.setGraphicTextGap(100);

Find the below working application.

LabelApp.java
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.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\\javaIcon.png";

 @Override
 public void start(Stage primaryStage) throws FileNotFoundException {
  InputStream is = new FileInputStream(imageFilePath);
  Image image = new Image(is);
  Label label = new Label("Java Icon", new ImageView(image));
  label.setGraphicTextGap(100);

  VBox vBox = new VBox(10, label);
  vBox.setAlignment(CENTER);

  Scene scene = new Scene(vBox, 600, 600);

  /* 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);
 }
}





Previous                                                 Next                                                 Home

No comments:

Post a Comment