Is it possible to style JavaFX components without CSS? I mean: can I achieve same what I could achieve with CSS without CSS? By "without CSS" I mean "without CSS strings" (stuff you put in setStyle(String)
.
It seems very inefficient to me to update style by creating new style strings. To dynamically change background color to white (not known beforehand) in following style I would have to replace it with regex or concate strings.
-fx-background-color: red;
-fx-border-color: black;
-fx-border-width: 1;
I believe that internally these strings will be parsed to some structures/objects anyway so why aren't these structures exposed?
I'm very new to JavaFX. I know I could use CSS inheritance but it isn't silver bullet and there would still be some string concatenation.
Much of the proposed API discussed in the original answer (e.g. CSS object model accessible via Java API and Java API for background definition) was implemented in subsequent years (see the JDK bug tracking issues and comments linked from the original answer for further information on that), as well as related javdoc:
As of JavaFX 2.2, there are some aspects of style which can only be accomplished through application of css. The definition of Region background colors and border information as you have in the question falls into the category of "things which can only be done via css".
You can style by defining different style classes in a css stylesheet and manipulating a Node's styleclasses. This is the recommended way to use css rather than using the setStyle method. You can also change the list of stylesheets applied to a scene or node hierarchy. However, this is really just using CSS in a different way rather than styling without css.
There are some aspects of style which can be done in fxml or java code because they are exposed in the JavaFX API, for instance, you can set a font on a label using the setFont api or the fxml font attribute, rather than applying a font to the label using a css style -fx-font.
In future JavaFX versions (e.g. JavaFX 8), many of the these things will also have a public API which you could use to apply styles in code without making use of CSS. In particular for JavaFX 8.0, new APIs have been defined which expose Region background and border styling properties for programmatic manipulation. See JDK-8102483: Public API for Region backgrounds and borders. This should allow you to programmatically accomplish the styling you example you provide in your question.
Also, proposed for future JavaFX versions is an object model of the CSS information so that CSS attributes on a node can be viewed and manipulated through a formal Java API in addition to the current string based setStyle and getStyleClass methods. See JDK-8091202: CSS Style Object Model in Java. If you are interested in such functionality, then vote for the feature request.