Remove Additive / Margin from JavaFX Tag

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

enter image description here

Source:

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();
    }
}
+5
source share
3 answers

This can be done by adding -fx-padding: -10 0 0 0;to your styles list.

For a more flexible solution, you can use the information FontMetrics:

FontMetrics metrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(label.getFont());
label.setPadding(new Insets(-metrics.getDescent(), 0, 0, 0));

NB: You need to call this code after scene.show(). Prior to this, the graphics engine is not ready to provide the correct indicators.

+5
source

- Text boundsType VISUAL. - , .

Text text = new Text();
text.setBoundsType(TextBoundsType.VISUAL);
+6

stage.show().

:

Separator separator = new Separator();
separator.setStyle(""
    + "-fx-border-width: 1px;"
    + "-fx-border-color: black;"
    + "-fx-padding: 0px;"
    + "");

stage.show()

Node line = separator.lookup(".line");
line.setStyle(""
    + "-fx-border-insets: 0px;"
    + "-fx-border-width: 0px;"
    + "");

, , 0, 0, calss .line

separator.getChildrenUnmodifiable().get(0).setStyle(""
    + "-fx-border-insets: 0px;"
    + "-fx-border-width: 0px;"
    + "");
0

Source: https://habr.com/ru/post/1620786/


All Articles