B4J Question B4XDialog Button

Guenter Becker

Active Member
Licensed User
Longtime User
Hello,
I like to localize the button text of a b4xDialog. At this moment I have the problem that the button text will not be shown complete.
1761817731652.png

I think I have to measure the text and I have to set the buttons width accordingly. I used several snippeds from the forum like:

B4X: Copy
 Private  Sub MeasureTexWidth(Text As String,ThisFont As Font) As Double
    Dim TextObj As JavaObject
    TextObj.InitializeNewInstance("javafx.scene.text.Text",Array(Text))
    TextObj.RunMethod("setFont",Array(ThisFont))
    TextObj.RunMethod("applyCss",Null)
    Dim Bounds As JavaObject
    Bounds = TextObj.RunMethodJO("getLayoutBounds",Null)
    Log(Bounds.RunMethod("getWidth",Null))
    Return Bounds.RunMethod("getWidth",Null)
End Sub

But this will not work correct. Any Idea how to display the text complete or how to stretch the button width depending on its text?
 
Code: Copy
'# GBEDialog = B4XDialog. Changed Private Buttonwidth to public
'# added measure text sub to B4XDialog code

'# code is called after creation of pane and loading layout for custom dialog
public Sub adjustButtons(dlg As  GBEDialog,p As Object,y As String, n As String, c As String)

    ' adjust buttons

    Dim bw As Int=0

    Dim bt As Int = 0

    #if B4J

        Dim p1 As Pane = p.As(Pane)

    #else if B4A

        Dim p1 As Panel = p.as(panel)

    #End If

    If c <> "" Then

        bt=bt+1

        bw = dlg.MeasureTextWidth(c,dlg.ButtonsFont) * 1.5

        If dlg.ButtonWidth < bw Then dlg.ButtonWidth=bw

    End If

    If n <> "" Then

        bt=bt+1

        bw = dlg.MeasureTextWidth(n,dlg.ButtonsFont) * 1.5

        If dlg.ButtonWidth < bw Then dlg.ButtonWidth=bw

    End If

    If y <> "" Then

        bt=bt+1

        bw = dlg.MeasureTextWidth(y,dlg.ButtonsFont) * 1.5

        If dlg.ButtonWidth < bw Then dlg.ButtonWidth=bw

    End If

    #if B4J

        If p1.PrefWidth < dlg.ButtonWidth * bt + 10dip Then

            p1.PrefWidth = dlg.ButtonWidth * bt + 10dip

        End If

    #else if B4A

        If p1.Width < dlg.ButtonWidth * bt + 10dip Then

            p1.Width = dlg.ButtonWidth * bt + 10dip

        End If

    #end if

End Sub


public Sub MeasureTextWidth(Text As String, Font1 As B4XFont) As Int
#If B4A
Private bmp As Bitmap
bmp.InitializeMutable(2dip, 2dip)
Private cvs As Canvas
cvs.Initialize2(bmp)
Return cvs.MeasureStringWidth(Text, Font1.ToNativeFont, Font1.Size)
#Else If B4i
Return Text.MeasureWidth(Font1.ToNativeFont)
#Else If B4J
Dim jo As JavaObject
jo.InitializeNewInstance("javafx.scene.text.Text", Array(Text))
jo.RunMethod("setFont",Array(Font1.ToNativeFont))
jo.RunMethod("setLineSpacing",Array(0.0))
jo.RunMethod("setWrappingWidth",Array(0.0))
Dim Bounds As JavaObject = jo.RunMethod("getLayoutBounds",Null)
Return Bounds.RunMethod("getWidth",Null)
#End If
End Sub
 
Upvote 0
 
Upvote 0
Thank you, think i placed the wrong question text to find this by myself.
This points to implement search AI in the future to better assistance.
 
Upvote 0
Back
Top