B4J Question css with treeview

madru

Active Member
Licensed User
Longtime User
Hi,

need a quick hand, how can I set the tree-cell color?

B4X:
.myRedTree .tree-cell {
      -fx-background-color:#ff0000;
      -fx-text-fill:#ffffff ;
}

THX
 

madru

Active Member
Licensed User
Longtime User
I thought this is correct but apparently not

B4X:
Treeview1.Style= "-fx-background-color: #00ff00; -fx-text-fill: #333333; -fx-Font-size: 12px; "

the tree-cell is left out :(
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

Example using an external CSS file.

Create the file project.css in the dirassets (=files) folder.
Note: This example contains several style settings to play around.
B4X:
.tree-view, .tree-cell {
    -fx-background-color:#ff0000;
  -fx-text-fill:#ffffff ;
}
.tree-cell:expanded, .tree-cell:sub-tree-item {
  -fx-background-color: #404040 ;
}
.tree-cell:selected {
  -fx-font-weight: bold ;
}
.tree-cell {
  -fx-padding: 0.75em 0em 0.75em 0em ;
}
.tree-cell:sub-tree-item {
  -fx-padding: 0.25em ;
}
For the project, add the file project.css to the files in the B4J IDE Files Tab.

Add the Stylesheet to the MainForm in AppStart:
B4X:
Sub AppStart (Form1 As Form, Args() As String)
...
   MainForm.Stylesheets.Add(File.GetUri(File.DirAssets, "project.css"))   
...
End Sub
 
Upvote 0

madru

Active Member
Licensed User
Longtime User
Hi Rob,

of course that will work, but I need to do it dynamically.

Based on the status of the App who controls some HeadEnd equipment I need to color the Tree dynamically. e.g. missed some packets color has to change to orange, missed all color has to change to red and so on. (just and example)

THX
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

this could be an option, using JavaObjects and the CSSUtils library, to do it dynamically - enhance if want to change the text color as well.
Tested the code on a simple list.

B4X:
'Set the cell background color of all treeview items (cells)
'Example: setTreeCellsColors(TreeView1, fx.colors.yellow)
Sub setTreeCellsColors(tv As TreeView, clr As Paint)
   Dim joTV As JavaObject = tv
   'Get all treecells
   Dim joTCL As JavaObject = joTV.RunMethodJo("lookupAll", Array(".tree-cell"))
   'Add those to an arraylist
   Dim joAL As JavaObject
   joAL.InitializeNewInstance("java.util.ArrayList", Array(joTCL))
   'Create a B4J list from the java arraylist
   Dim l As List = joAL
   'Set the color of the treecells
   For i = 0 To l.size - 1
     CSSUtils.SetBackgroundColor(l.Get(i), clr)
     ' CSSUtils.SetStyleProperty(l.Get(i), "-fx-text-fill", "red")  ' enhance if text color needs to change
   Next
End Sub

[ATTACH=full]54936[/ATTACH]
 

Attachments

  • upload_2017-4-20_14-48-25.png
    upload_2017-4-20_14-48-25.png
    3.4 KB · Views: 144
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Hi Rob,

Can this be used to change the background color of individual cells on a tree as it is created.
ie if the value of the cell is < a value change to Green or > change to Red etc?

Thanks
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Sure - amended and tested.
B4X:
'Set the background color of a treeview cell depending its value.
'Note: Ensure to expand all items and use subdelayed after treeview been build
'Example: CallSubDelayed3(Me, "SetTreeCellsBackgroundColor2", TreeView1, 20)
Sub SetTreeCellsBackgroundColor2(tv As TreeView, threshold As Int)   'ignore
   Dim joTV As JavaObject = tv
   'Get all treecells
   Dim joTCL As JavaObject = joTV.RunMethodJo("lookupAll", Array(".tree-cell"))
   'Add those to an arraylist
   Dim joAL As JavaObject
   joAL.InitializeNewInstance("java.util.ArrayList", Array(joTCL))
   'Create a List
   Dim l As List = joAL
   'Set the color of the treecells
   For i = 0 To l.size - 1
     'Get the treecell
     Dim joIP As JavaObject = l.Get(i)
     'Get the treeitem belonging to the treecell
     Dim ti As TreeItem = joIP.RunMethod("getTreeItem", Null)
     'Check if treeitem is initialized and a number - f.e. if root is visible then not a number
     If ti.IsInitialized And IsNumber(ti.Text) Then
       'Set the cell color depending threshold
       If ti.Text < threshold Then
         CSSUtils.SetBackgroundColor(l.Get(i), fx.Colors.green)
       Else
         CSSUtils.SetBackgroundColor(l.Get(i), fx.Colors.red)
       End If
     End If
   Next
End Sub

upload_2017-4-21_11-44-54.png
 
Upvote 0
Top