0

I need to apply CSS to change collapse/expand icons for a particular treeview in my JavaFX application. FX:ID for it is treeView. However I have many other treeviews in my application and I don't want those icons to be changed.

Change expand and collapse image TreeView JavaFX 2.2

Above link provides a solution to apply CSS but that would change icons in all the treeViews. Is there any way I can apply CSS to particular treeView?

Community
  • 1
  • 1
NaveenBharadwaj
  • 1,212
  • 5
  • 19
  • 42

1 Answers1

1

You can assign a separate class to your TreeView and change its icon.

treeView.getStyleClass().add("my-tree-view");

and change the icons only for this treeView

.my-tree-view > .virtual-flow> .clipped-container > .sheet > .tree-cell .tree-disclosure-node .arrow {
    -fx-background-color: red;
}
.my-tree-view > .virtual-flow> .clipped-container > .sheet > .tree-cell:expanded .tree-disclosure-node .arrow {
    -fx-background-color: blue;
}
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • I had this idea..but this doesn't seem to work :( I'm opening another stage as a popup from my main stage. And this treeview is present in the new stage. Anything needs to be done to handle that? – NaveenBharadwaj Jun 09 '15 at 08:36
  • You need to load the stylesheet and add it to either the scene of the new Stage or directly to the treeview. – ItachiUchiha Jun 09 '15 at 08:41
  • You're right. I had forgotten to load the css. However after adding, other styles are getting reflected. But not the treeview one. – NaveenBharadwaj Jun 09 '15 at 08:45
  • What style did you use? – ItachiUchiha Jun 09 '15 at 08:48
  • scene.getStylesheets().add("styles.css"); I loaded the css with the above..then I added treeView.getStyleClass().add("my-tree-view"); in the controller. In styles.css, I added the code that is there in your answer. – NaveenBharadwaj Jun 09 '15 at 08:50
  • I haven't tried this, but it should work. Can you try the MCVE in the question? – ItachiUchiha Jun 09 '15 at 09:02