B4J Question Desktop / Win app - how to find an object

GregP

New Member
Hi all!

I am very new here, and I hope that someone can help me.
Maybe an easy think, but I cannot find the solution:

if I have some Labels and one seekbar in the same form, and I want to change only one texts according to the seekbar.value how can I do that? The seekbar.value is the selector, which Label.Text needs to change.

The worst way the if-then statement.
But maybe existing a better way to select the correct Label object, maybe with their Tag? I don’t know.

any idea?
 

GregP

New Member
I'll try to make it clear:

I've a Form with 5 labels, 1 button and 1 spinner

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Label1 As Label       'Label1.Tag = 1
    Private Label2 As Label       'Label2.Tag = 2
    Private Label3 As Label       'Label3.Tag = 3
    Private Label4 As Label       'Label4.Tag = 4
    Private Label5 As Label       'Label5.Tag = 5
       
    Private Spinner1 As Spinner
End Sub

With Button click, the selected Label object get a new .Text value:

B4X:
Sub Button1_Click
Dim s As String
s= Spinner1.Value

For Each v As Label In Root.GetAllViewsRecursive

    If v.Tag = s Then
        v.Text = "Selected Label: " & s
    End If
Next
   
End Sub
In Debug mode, if I set a breakpoint within the If statement (line 7-9 above), it's working fine, but in Release mod, or without breakpoint it doesn't.
 
Last edited:

Jorge M A

Well-Known Member
Licensed User
Try with Sleep(0) after updating v.Text
B4X:
    If v.Tag = s Then
        v.Text = "Selected Label: " & s
        Sleep(0)   'Wait for Refresh Display
    End If
You can also use a Views Array
ie:
B4X:
Sub Process_Globals
    Private fx As JFX
    Dim MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView

    Private Label1 As Label
    Private Label2 As Label
    Private Label3 As Label
    Private Label4 As Label
    Private Label5 As Label
      
    Private Spinner1 As Spinner
    
    Private labels(5) As Label ' Views Array
    
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    
    labels(0)=Label1
    labels(1)=Label2
    labels(2)=Label3
    labels(3)=Label4
    labels(4)=Label5
    
    MainForm.Show
    
End Sub

Sub Spinner1_ValueChanged (Value As Object)
    Log(Value)
    labels(Value - 1).Text = "Selected Label: " & Value
    
    Sleep(0)            'Wait for Refresh Display
    
End Sub
 

GregP

New Member
Using Sleep(0) would be nicer solution, especially if I'd like to use more than 5 Label.
But still no working.
Error message:

b4xmainpage$ResumableSub_Button1_Click.resume (java line: 94)
java.lang.ClassCastException: class javafx.scene.control.Button cannot be cast to class javafx.scene.control.Label (javafx.scene.control.Button and javafx.scene.control.Label are in module javafx.controls of loader 'app')
at b4j.example.b4xmainpage$ResumableSub_Button1_Click.resume(b4xmainpage.java:94)
at b4j.example.b4xmainpage._button1_click(b4xmainpage.java:48)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.BA$1.run(BA.java:216)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
 

Erel

Administrator
Staff member
Licensed User
Sleep is not relevant here.

This code is wrong:
B4X:
For Each v As Label In Root.GetAllViewsRecursive
It will try to cast all views to a label and fail.

Correct code:
B4X:
Sub Button1_Click
 Dim s As String = Spinner1.Value
For Each v As B4XView In Root.GetAllViewsRecursive
    If v.Tag = s Then
        v.Text = "Selected Label: " & s
    End If
Next 
End Sub
 

GregP

New Member
solution:
For Each v As B4XView In Root.GetAllViewsRecursive
Super, working fine!

Thank you so much!
 
Top