0

I have a JavaFX application. Some Tabs in a TabPane have a class "my-view", and inside there is a complex structure, featuring labels, textfields and so on. I have a CSS rule to set the font size on these sub-items like:

.my-view .label, .my-view .my-text-field-a, .my-view .my-text-field-b {
    -fx-font-size: 12px;
}

I want to allow the user to press Ctrl-+ to dynamically increase text size. What I really want is to be able to dynamically adjust the "12px" in the above CSS, dynamically. However, I can't see a way to do this in JavaFX. The setStyle method only allows setting the style for the current node, not a rule as above. The new JavaFX 8 StyleableProperty interface seems to go in the wrong direction for me -- it allows code to read but not write CSS properties. I don't really want to have to find all the relevant text fields myself in the code and call setFont; CSS rules seem like the right mechanism here.

The only way I can currently think to do it is to create a new class for each font size that I might want to set (a bit like suggested here: Hot to update dynamically font size) and then set the appropriate class to set the size, e.g.

.my-view-12 .label, .my-view-12 .my-text-field-a, .my-view-12 .my-text-field-b {
    -fx-font-size: 12px;
}

.my-view-14 .label, .my-view-14 .my-text-field-a, .my-view-14 .my-text-field-b {
    -fx-font-size: 14px;
}

I can autogenerate the CSS (for all font sizes say 1-100), but this method just seems like an ugly hack. Are there any better ways to do this in JavaFX?

2
  • Is everything with text displayed inside the tabs with class "my-view" using the same (changeable) font size? Commented May 22, 2014 at 11:04
  • At the moment, yes, though that might not remain the case (hence my use of the my-text-field-a classes rather than text-field). Is there an easier solution in that case? I did try playing with the "inherit" value for -fx-font-size but got a lot of errors spat out on the terminal. Commented May 22, 2014 at 11:16

1 Answer 1

3

If everything with text displayed under the nodes with "my-view" style class is going to use the same font, then you can just do

.my-view {
    -fx-font-size: 12 px ;
}

in the css file.

To update, choose some node that's an ancestor of all your nodes which have the "my-view" style class (it sounds like your tab pane will work, but in general you could just use the root node of the scene) and do

for (Node n: tabPane.lookupAll(".my-view")) {
    n.setStyle("-fx-font-size: "+fontSize+"px;");
}

You obviously need to keep track of the font size in the Java code, which is simple enough (just do it from the outset).

If only a subset of the text-displaying nodes are going to have their font sized changed, you will likely have to give them a style class of their own, then do a lookup for that specific style class, instead of "my-view".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.