user4398698
user4398698

Reputation:

Override JavaFX CSS style in code

Is it possible to override a JavaFX CSS definition in code?

For example, in my CSS file I defined a button's height as follows:

.button {
    -fx-pref-height: 30px;
}

When I try to override this height for some button in code...

Button button = new Button();
button.setPrefSize(100, 50); // width, height

...the button still has a height of 30 pixels.

Upvotes: 1

Views: 2338

Answers (1)

eckig
eckig

Reputation: 11154

There are two solutions:

  1. Make sure that button.setPrefSize(100, 50); // width, height is called after the Button became visible.
  2. Use Bindings to force its size (CSS does not overwrite bound values), for example: button.prefHeightProperty().bind(new SimpleDoubleProperty(60));

Upvotes: 3

Related Questions