Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove padding/margin from JavaFX Label

Is there a way to remove the default space (padding/margin) that JavaFX label adds? I want to get rid of the space displayed between the black lines on the image below:

enter image description here

Source code:

public class LabelTest extends Application
{

    @Override
    public void start(final Stage primaryStage)
    {
        final Group root = new Group();
        final Scene scene = new Scene(root, 300, 130, Color.WHITE);

        final GridPane gridpane = new GridPane();
        gridpane.setPadding(new Insets(5));
        gridpane.setHgap(10);
        gridpane.setVgap(10);

        final Label label = new Label("Label");
        label.setStyle("-fx-font-size:44px;-fx-font-weight: bold;-fx-text-fill:#5E34B1;-fx-background-color:#ffc300;");
        GridPane.setHalignment(label, HPos.CENTER);
        gridpane.add(label, 0, 0);

        root.getChildren().add(gridpane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
like image 861
SDS Avatar asked Dec 18 '15 16:12

SDS


1 Answers

One of the more dynamic ways to do this is to use a Text instead of a Label and set the boundsType as VISUAL. This results in a Text without any padding on the any of the sides of the Text, irrespective of the font size.

Text text = new Text();
text.setBoundsType(TextBoundsType.VISUAL);
like image 157
ItachiUchiha Avatar answered Sep 17 '22 12:09

ItachiUchiha