Reputation: 6776
I am programming a JavFX UI on Java 8 where I create Buttons with sizes calculated at construction time.
The problem is that the button's text (usually just a single letter in my case) sometimes is too big to fit in the button and then the button just stays blank.
How can I set the font size for a JavaFX Button so that the text is not too high to not get displayed?
Upvotes: 5
Views: 6146
Reputation: 6776
I found a solution. It's probably not very beautiful and I have no idea where the 0.45
come from (found it by trying), but it seems to work for me:
int buttonSize = 70;
Button button = new Button("A");
button.setMinSize(buttonSize, buttonSize);
button.setPrefSize(buttonSize, buttonSize);
button.setMaxSize(buttonSize, buttonSize);
button.setStyle(String.format("-fx-font-size: %dpx;", (int)(0.45 * buttonSize)));
The last line calculates the font height in px
units and sets it as style for this button.
Upvotes: 2