1

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.

3
  • 1
    It should work. Can you create a MCVE? Commented Aug 17, 2015 at 10:02
  • 3
    @Rvervuurt: It's Java, not HTML Commented Aug 18, 2015 at 12:34
  • MCVE: bikusta.de/wp-content/uploads/2015/08/javafx-css-mcve.zip Commented Aug 18, 2015 at 12:35

1 Answer 1

3

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));
Sign up to request clarification or add additional context in comments.

1 Comment

The second solution is working. I also found a third one: button.setStyle("-fx-pref-height: 60px");

Your Answer

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