After loading a layout with labels and a textarea, I am adding blocks of labels, via code, according to this code:
B4X:
Dim myWidth As Int
myWidth=15dip
For y=1 To 2
For x=1 To 14
Dim cLabel As Label
cLabel.Initialize("")
cLabel.Tag=block & "," & mynames(y,x)
'Log(cLabel.Tag)
Gauge.RootPane.AddNode(cLabel,l+(x*(myWidth+2)),t+((y-1)*(myWidth+2)),myWidth, myWidth)
Dim r, g, b As Int
Dim RGBColor() As String
RGBColor=Regex.Split(",", mycolors(y,x))
r=RGBColor(0)
g=RGBColor(1)
b=RGBColor(2)
cLabel.Style=$"-fx-background-color:rgb(${r},${g},${b});"$
Next
Next
Each label has a tag in the form "x,color name" (eg. "1,blue"). I need to loop through the labels and find the tag for a particular block and color and then change its border to make it black.
My problem is the loop.
I tried using:
B4X:
For Each lbl As Label In Gauge.RootPane.GetAllViewsRecursive
Log(lbl.Tag)
If lbl.Tag="1," & row(2) Then
lbl.Style="-fx-border-width: 2;"
End If
Next
but this crashes when it finds the textarea, with a ClassCastException. I know the loop is wrong but I am stuck. Any help is welcome.
For Each n As Node In Gauge.RootPane.GetAllViewsRecursive
If n Is Label Then
Dim lbl As Label = n
Log(lbl.Tag)
If lbl.Tag="1," & row(2) Then
lbl.Style="-fx-border-width: 2;"
End If
End If
Next
For Each n As Node In Gauge.RootPane.GetAllViewsRecursive
If n Is Label Then
Dim lbl As Label = n
Dim CompareText As String="1," & row(2)
'Log(lbl.Tag)
If lbl.Tag=CompareText Then
'lbl.Style="-fx-border-width: 2;"
lbl.Style=$"-fx-border-width: 2; -fx-background-color: ${row(2).Trim};"$
End If
End If
Next
I know I am finding the right label as I can change its color to black (from blue) but I cannot seem to get the border right. In this case, the value in row(2) is " blue" with a leading space - hence trim.
For Each n As Node In Gauge.RootPane.GetAllViewsRecursive
If n Is Label Then
Dim lbl As Label = n
Dim CompareText As String="1," & row(2)
'Log(lbl.Tag)
If lbl.Tag=CompareText Then
'lbl.Style="-fx-border-width: 2;"
lbl.Style=$"-fx-border-color: black; -fx-border-width: 2; -fx-background-color: ${row(2).Trim};"$
End If
End If
Next
When I add the labels I set the border color to white and width to 0. Now it works. Many thanks.
The style property is used by the designer and possibly in other cases to set all kinds of attributes. When you set the Style property directly you remove the previous style.
CSSUtils only updates the specific property and keeps the existing style.