0

I would like to have some of my TreeItems with different font, italic or color and I see that perhaps TreeCell in JavaFX should be used somehow. I want to apply this at the moment I create the TreeItem, i.e. when I do

root.getChilden().add(...) 

I would imagine that there should be a way to access the style properties for this TreeItem and apply some set methods accordingly.

Regards

Update : this is more or less what I want to achieve

1.root is created 2. Add a child test i. e. root.getChildren.add("test") 3.set this child italic and red 4. Add another child test1 5 set the child test1 to font size 12 and bold

Chavi
  • 31
  • 1
  • 4
  • Usually you'd simply store this kind of info in the item and modify your the style of the `TreeCell` you return from a custom `cellFactory` accordingly on changes of the item... – fabian Nov 04 '19 at 17:51
  • 2
    You don't control when `TreeCell`s are created. Also, `TreeCell`s are reused. If you want to customize a `TreeCell` you return a subclass from a `cellFactory` and override the `Cell#updateItem(T,boolean)` method. – Slaw Nov 04 '19 at 19:52
  • Thank you Fabian for fixing my text :) – Chavi Nov 04 '19 at 21:45
  • Fabian, Slaw, could you please point me to an example on this? So far I could not find. I put an update what I want to achieve – Chavi Nov 04 '19 at 21:50
  • 1
    How are the appropriate styles determined? Your description makes it sound random but there should be some pattern or model property which determines what styles a cell should have. – Slaw Nov 04 '19 at 22:15
  • At the moment I don't think I have styles. I am parsing some data and want to adjust the formatting of different tree items. It is true that perhaps I can define 3-5 different styles and then set different properties. This is where it gets fuzzy for me. – Chavi Nov 07 '19 at 16:30
  • A `TreeView` has a reference to a `TreeItem` tree but those `TreeItem`s are displayed via a `TreeCell`. At any given time a `TreeView` will only have a max of about 30 `TreeCell` instances and those instances are reused as the user scrolls. That's why you have to update the styles, if appropriate, in the `#updateItem` method. So if you want random styles just set the properties to random values in said method. However, if you want each to have persistent styles based on the `TreeItem` currently in the `TreeCell` you'll have to store that information. – Slaw Nov 07 '19 at 21:45
  • Typically, however, the styles are determined by the state of the model. For example, if your model has a concept of "read-only" then maybe the `TreeCell` would set the text fill to red when holding a read-only model item, else the text fill is left as the default color. To make this easier to implement you could set up a custom `PseudoClass` and then style the `TreeCell` from CSS. – Slaw Nov 07 '19 at 21:45

0 Answers0