JavaFX2: What is the best way to style an individu

2019-06-10 05:02发布

问题:

I would have liked to have the ability to add a CSS class to the TreeItem and then just use a CSS file to give it style. But that is not possible, or is it?

Is there a way to add a class like we can using getStyleClass()?

If not, what is the best way to give individual items a style?

回答1:

Best way to give style to items is use of stylesheets, limit use of Node.setStyle.

You may wonder why?

Reason is a performance. Calculate style values are cached. All Nodes that match the same selectors can share the calculculate value.



回答2:

Well... Think outside the box! You can't style treeitem and tablerow because they act basically as a form of containers to other nodes, so set a class for the nodes on your treeitem (or tablerow) and then style them. I'm sorry by the answer, but I can't add comments yet



回答3:

Use a TreeCellFactory , and in the factory, use a Node and set it's style, either via getStyle, or even better via Node.setStyleClass.