LeTadas
LeTadas

Reputation: 3566

JavaFX ImageView with label in front of it

I want to have ImageView which would have label in front of it at centre, but can't figure out which parent layout to use to achieve this.

What I tried so far:

<AnchorPane ...>
<children>
    <Pane layoutX="350" layoutY="270" prefHeight="300" prefWidth="300">
        <children>
            <ImageView fitHeight="300" fitWidth="300">
                <image>
                    <Image url="someimage.png" />
                </image>
            </ImageView>
        </children>
    </Pane>
    <Pane layoutX="350" layoutY="270" prefHeight="300" prefWidth="300">
        <children>
            <Label alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" text="num" />
        </children>
    </Pane>
    <more elements/>
 </children>

Example what I want (only heart with text):

enter image description here

Upvotes: 0

Views: 1116

Answers (1)

Sergey Grinev
Sergey Grinev

Reputation: 34528

Use StackPane:

    StackPane root = new StackPane();
    root.getChildren().addAll(
            new ImageView("https://upload.wikimedia.org/wikipedia/commons/thumb/4/42/Love_Heart_SVG.svg/200px-Love_Heart_SVG.svg.png"),
            new Label("HEART RATE"));

    Scene scene = new Scene(root, 300, 250);

enter image description here

Upvotes: 4

Related Questions