Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align icon and text inside the big button?

Tags:

javafx-2

I have quite a big button (minWidth and minHeight were explicitly set to big numbers), and inside that big button there is relatively small icon and some text. Icon and text do not consume all available space, and end up being placed in the center of the button.

I want to put icon and text to the left side of the button. But it seems that I do not understand what all those alignments mean, since setting alignment to BASELINE_LEFT or setting textAlignment to LEFT didn't change anything.

How can I fix it?

like image 305
Rogach Avatar asked Jul 22 '12 09:07

Rogach


People also ask

How do I align text and icon on same line?

If you want to make a text appear vertically aligned next to a Font Awesome icon, you can use the CSS vertical-align property set to “middle” and also, specify the line-height property. Change the size of the icon with the font-size property.

How do I align text inside a button?

We can center the button by using the following methods: text-align: center - By setting the value of text-align property of parent div tag to the center. margin: auto - By setting the value of margin property to auto.

Does text-align work on buttons?

The text-align property applies to the horizontal placement of the text within the button, not the alignment of the button itself.


1 Answers

Property textAlignment controls alignment for multiline text so it wouldn't help you.

But both

 btn.setStyle("-fx-alignment: LEFT;");

or

 btn.setAlignment(Pos.BASELINE_LEFT);

should work for you. See example below.

enter image description here

public class ILoveBigButtonsAndICannotLie extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Button");
        btn.setGraphic(new Rectangle(10,10, Color.RED));
        btn.setMinHeight(200);
        btn.setMinWidth(250);
        //btn.setStyle("-fx-alignment: LEFT;");
        btn.setAlignment(Pos.BASELINE_LEFT);

        StackPane root = new StackPane();
        root.getChildren().add(btn);
        primaryStage.setScene(new Scene(root, 300, 250));
        primaryStage.show();
    }

    public static void main(String[] args) { launch(); }
}
like image 157
Sergey Grinev Avatar answered Nov 07 '22 08:11

Sergey Grinev