cefeboru
cefeboru

Reputation: 332

Set JavaFx ComboBox Font?

Im trying to change a ComboBox Font on JavaFx, so I have:

ComboBox cbCategoria = new ComboBox();

Im new in javaFx so some example code would be great :D, Is a Way to do it without CSS? and if not how can i make it with CSS,I havent learned how to use CSS Styling yet :(

Upvotes: 4

Views: 12166

Answers (3)

user12406016
user12406016

Reputation:

// I havent learned how to use CSS Styling yet :( //

--> Then you can try this

ComboBox cbCategoria = new ComboBox();
cbCategoria.getEditor().setFont(Font.font("Verdana", FontWeight.EXTRA_BOLD, 14));

Upvotes: 1

Shekh Shagar
Shekh Shagar

Reputation: 1355

VBox vbox = new VBox(10);
vbox.setAlignment(Pos.CENTER_LEFT);

ComboBox<String> noStyled = new ComboBox<>();
noStyled.getItems().addAll("One", "Two", "Three");

ComboBox<String> styled = new ComboBox<>();
styled.setPrefWidth(150);
styled.getItems().addAll("One", "Two", "Three");
styled.setStyle("-fx-font: 30px \"Serif\";");

vbox.getChildren().addAll(noStyled, styled);
Scene scene = new Scene(vbox);
stage.setScene(scene);
stage.show();

Just for suggetion. if you want to set size of combobox using above code.you should try like this

styled.setPrefWidth(150);
styled.setMinHeight(30);

Instead of,

 styled.setPrefSize(150,30);

If you set like this, you'll get exception. I faced hard time about this. Hope it'll be helpful.

Upvotes: 2

Antonio J.
Antonio J.

Reputation: 1760

I think there is no way to do it without CSS. You can assign the style to that component as in the next sample:

VBox vbox = new VBox(10);
vbox.setAlignment(Pos.CENTER_LEFT);

ComboBox<String> noStyled = new ComboBox<>();
noStyled.getItems().addAll("One", "Two", "Three");

ComboBox<String> styled = new ComboBox<>();
styled.setPrefWidth(150);
styled.getItems().addAll("One", "Two", "Three");
styled.setStyle("-fx-font: 30px \"Serif\";");

vbox.getChildren().addAll(noStyled, styled);
Scene scene = new Scene(vbox);
stage.setScene(scene);
stage.show();

Or you can assign a stylesheet to an application. In both cases I recommend you to go through the tutorial CSS Section in the oracle web site and the reference guide.

Hope it helps.

Upvotes: 7

Related Questions