Featured Post

Applying Email Validation to a JavaFX TextField Using Binding

This example uses the same controller as in a previous post but adds a use case to support email validation.  A Commons Validator object is ...

Tuesday, June 30, 2015

Clearing JavaFX AnchorPane Padding

Unlike other containers in JavaFX, AnchorPane has a padding set by default through its stylesheet.  This blog post shows how to reset this value in SceneBuilder to produce an unpadded TextArea.

In SceneBuilder when you add a JavaFX control like a TextArea to an AnchorPane and set the anchor points to 0, you'll get padding.  This is despite the padding setting being cleared in the Properties docker.

The following SceneBuilder screenshot shows a TextArea in an AnchorPane.  The TextArea's Anchor Pane Constraints are all set to zero.  Padding is also set to zero.  However, notice the highlighted value of the insets (9.6).

TextArea in AnchorPane with Zero Padding Values But Positive Insets

Apparently, the default stylesheet sets a -fx-padding value for all AnchorPanes.  It's best to counter this with your own stylesheet, but if you need a one-off, set this value for your particular AnchorPane using a style element.

This screenshot shows the unpadded TextArea.  A -fx-padding value of 0 has been added which overrides the default stylesheet.

AnchorPane with Embedded Style Overriding Default Stylesheet

Prefer using your own styles in stylesheets rather than embedding a style in the FXML.  You don't need to change the rule in the default stylesheet, just supply your more specific value using a class or even an id selector which will take precedence over the general rule.  But if you need a quick fix, add in the -fx-padding.

No comments:

Post a Comment