B4J Question label center justify doesn't work

techknight

Well-Known Member
Licensed User
Longtime User
No matter how hard i try, I cannot get the center-justify to work at all when adding labels by code.

Here is my code block which tries to set the properties of the label:

B4X:
'Generates a label with all the parameters required for such
Sub AddLabel(labelobject As Label, panelobject As Pane, Params As LabelParams)
    labelobject.Initialize("")
    labelobject.TextColor = fx.Colors.From32Bit(Params.TextColor)
    CSSUtils.SetBackgroundColor(labelobject, fx.Colors.From32Bit(Params.Color))
    CSSUtils.SetStyleProperty(labelobject, "-fx-text-alignment", "center")
    labelobject.Text = Params.Text
    labelobject.WrapText = False
    Dim jo As JavaObject = labelobject
    jo.RunMethod("setTextAlignment", Array("CENTER"))
    labelobject.TextSize = Params.TextSize
    Log(CSSUtils.GetStyleProperty(labelobject, "-fx-text-alignment"))
    panelobject.AddNode(labelobject, Params.Left, Params.Top, Params.Width, Params.Height)
End Sub

the code which calls the above code:
B4X:
'Draw the Period/Quarter/Inning/Half etc.
Private Sub Draw_period(ArgumentMap As Map)
    Dim Property As ControlProperties = ArgumentMap.Get("Property")

    Views.lblPeriod.Initialize("")  
    Dim lParams As LabelParams = Common.CreateLabelParams(Property.Size, Common.StringToInt(Property.TextColor), BackColor, Property.DisplayName, True, Property.Left, Property.Top, Property.Width, Property.Height)
    Common.AddLabel(Views.lblPeriod, ScorePanel, lParams)  
    LoadFont(Views.lblPeriod, Property.FontID)
End Sub

Notice, I try to set the justify in 2 different places but no matter what, it just doesn't work. I added a background color to make the issue better stand out:
1719944878954.png



I tried to log the style property to see if its returning center, and it does. but ONLY when i set it, otherwise it returns empty.

Not sure where to go from here, thoughts?
 

techknight

Well-Known Member
Licensed User
Longtime User
The answer struck me the minute i started googling stackexchange on Java, its setAlignment and not setTextAlignment.

Solved.
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
You can try this.

Center label:
WrapLabel(yourlabel, "BASELINE_CENTER", 25),
'Set Label Aligement height is used for center the Text
Public Sub WrapLabel(lbl As Label, Alignment As String, NodeHeight As Double) As Pane
    Try
        Dim pn1 As AnchorPane
        pn1.Initialize("")
        pn1.AddNode(lbl, 0, 0, -1, NodeHeight -1)
        pn1.FillHorizontally(lbl, 0, 0)
        Dim jo1 = lbl As JavaObject
        jo1.RunMethod("setAlignment", Array As Object(Alignment))
        Return pn1
    Catch
        Log("Error Wrablabel")
    End Try
End Sub
 
Upvote 0
Top