0

I have a button with an icon and text and I can't figure out how to place the icon in the center and the text in the bottom left of the button. This is what it looks like right now:

<Button text="Text" GridPane.rowIndex="0" GridPane.columnIndex="2" prefWidth="135" prefHeight="110" style="-fx-background-color: rgb(176,30,0); -fx-text-fill: rgb(255,255,255); -fx-alignment: BOTTOM_LEFT;">
    <graphic>
        <ImageView>
            <Image url="file:///....png"/>
        </ImageView>
    </graphic>
</Button>

Two things I've tried, which haven't been working: 1. Giving the ImageView a separate alignment attribute. 2. Placing a VXbox inside the button and the imageView as well as label with text inside the VBox.

Thanks in advance!

1 Answer 1

3

The contentDisplay property controls the position of the graphic relative to the text. The alignment property controls the position of the overall content.

So

<Button text="Text" GridPane.rowIndex="0" GridPane.columnIndex="2" prefWidth="135" prefHeight="110" 
    alignment="BOTTOM_LEFT" contentDisplay="TOP" 
    style="-fx-background-color: rgb(176,30,0); -fx-text-fill: rgb(255,255,255);">

or some similar combination, should work.

If you prefer to set these in CSS, -fx-alignment: bottom-left; -fx-content-display: top ; will do the same.

The contentDisplay property doesn't quite give you full control over the positioning of the graphic; you may need to use the idea you proposed of placing a container with the image view and a label as the graphic. In this case, you probably want an alignment of center. A BorderPane would probably be closer to what you need than a VBox:

<Button GridPane.rowIndex="0" GridPane.columnIndex="2" prefWidth="135" prefHeight="110" style="-fx-background-color: rgb(176,30,0); -fx-text-fill: rgb(255,255,255); -fx-alignment: center;">
    <graphic>
        <BorderPane>
            <center>
                <ImageView>
                    <Image url="file:///....png"/>
                </ImageView>
            </center>
            <bottom>
                <Label text="Text"/>
            </bottom>
        </BorderPane>
    </graphic>
</Button>

You may also need to experiment with setting the preferred size of the border pane, to make it fill the area allocated to the button.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but unfortunately, alignment="CENTER" places the image at the center of the text instead of the very center of the button. Any idea how to fix this?
@Varest In that case, you probably need to revert to the idea of using a container as the graphic (see update for suggestion). You could also experiment, e.g., with wrapping the image view in a HBox with preferred width and alignment set on the HBox.
Your second suggestion worked perfectly, thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.