B4J Question [Solved or found!] Trying to find a label

Mark Read

Well-Known Member
Licensed User
Longtime User
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.
 

stevel05

Expert
Licensed User
Longtime User
Try:
B4X:
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
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Sorry edited as saved the answer too early :)
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Thanks Steve. I tried this:

B4X:
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.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Have you set the border color? It may be getting overwritten as you are setting the lbl.Style directly.

I would try using CSSUtils to set these parameters.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
OMG I forgot.

B4X:
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.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
This is a mistake to set the Style property directly. You should use CSSUtils instead.

You are correct (as usual!). You do not say why it is a mistake but it is easier. Thank you.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You do not say why it is a mistake but it is easier.
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.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Thanks for the explanation. That was exactly my problem. Have changed to CSSUtils.
 
Upvote 0
Top