B4J Tutorial eu/hansolo/fx/spinner

It is based on this Github posting. But could most probably be done in a far more efficient way than what I have done.

Jar attached - copy it to your B4J additional libs folder
B4J sample attached - extract with z-zip and then open and run the B4J project

Startup screen
1758372898405.png

Intermediate screen
1758373001655.png

Final screen
1758373058977.png


Sample Code:
Spinner:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600

#End Region

#AdditionalJar: OdoMeterJHS

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    
    Dim sp0, sp1, sp2, sp3, sp4, sp5, sp6, sp7, sp8, sp9, sp10, sp11, sp12, sp13, sp14, sp15 As JavaObject
    
    Dim m As Map
    
    Dim mysting As String
    
    
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    
    
    mysting = "SPINNER_BY_JOHAN"
    
    
    Dim val(27) As Double
    For i = 0 To 26
        val(i) = i
    Next
    
    m.Initialize
    m.Put("A", val(0))
    m.Put("B", val(1))
    m.Put("C", val(2))
    m.Put("D", val(3))
    m.Put("E", val(4))
    m.Put("F", val(5))
    m.Put("G", val(6))
    m.Put("H", val(7))
    m.Put("I", val(8))
    m.Put("J", val(9))
    m.Put("K", val(10))
    m.Put("L", val(11))
    m.Put("M", val(12))
    m.Put("N", val(13))
    m.Put("O", val(14))
    m.Put("P", val(15))
    m.Put("Q", val(16))
    m.Put("R", val(17))
    m.Put("S", val(18))
    m.Put("T", val(19))
    m.Put("U", val(20))
    m.Put("V", val(21))
    m.Put("W", val(22))
    m.Put("X", val(23))
    m.Put("Y", val(24))
    m.Put("Z", val(25))
    m.Put("_", val(26))

    Dim spt As JavaObject
    spt.InitializeStatic("eu.hansolo.fx.spinner.SpinnerType")
    
    sp0.InitializeNewInstance("eu.hansolo.fx.spinner.CanvasSpinner", Array(spt.GetField("ALPHABETIC")))
    sp1.InitializeNewInstance("eu.hansolo.fx.spinner.CanvasSpinner", Array(spt.GetField("ALPHABETIC")))
    sp2.InitializeNewInstance("eu.hansolo.fx.spinner.CanvasSpinner", Array(spt.GetField("ALPHABETIC")))
    sp3.InitializeNewInstance("eu.hansolo.fx.spinner.CanvasSpinner", Array(spt.GetField("ALPHABETIC")))
    sp4.InitializeNewInstance("eu.hansolo.fx.spinner.CanvasSpinner", Array(spt.GetField("ALPHABETIC")))
    sp5.InitializeNewInstance("eu.hansolo.fx.spinner.CanvasSpinner", Array(spt.GetField("ALPHABETIC")))
    sp6.InitializeNewInstance("eu.hansolo.fx.spinner.CanvasSpinner", Array(spt.GetField("ALPHABETIC")))
    sp7.InitializeNewInstance("eu.hansolo.fx.spinner.CanvasSpinner", Array(spt.GetField("ALPHABETIC")))
    sp8.InitializeNewInstance("eu.hansolo.fx.spinner.CanvasSpinner", Array(spt.GetField("ALPHABETIC")))
    sp9.InitializeNewInstance("eu.hansolo.fx.spinner.CanvasSpinner", Array(spt.GetField("ALPHABETIC")))
    sp10.InitializeNewInstance("eu.hansolo.fx.spinner.CanvasSpinner", Array(spt.GetField("ALPHABETIC")))
    sp11.InitializeNewInstance("eu.hansolo.fx.spinner.CanvasSpinner", Array(spt.GetField("ALPHABETIC")))
    sp12.InitializeNewInstance("eu.hansolo.fx.spinner.CanvasSpinner", Array(spt.GetField("ALPHABETIC")))
    sp13.InitializeNewInstance("eu.hansolo.fx.spinner.CanvasSpinner", Array(spt.GetField("ALPHABETIC")))
    sp14.InitializeNewInstance("eu.hansolo.fx.spinner.CanvasSpinner", Array(spt.GetField("ALPHABETIC")))
    sp15.InitializeNewInstance("eu.hansolo.fx.spinner.CanvasSpinner", Array(spt.GetField("ALPHABETIC")))
    
    Form1.RootPane.AddNode(sp0, 10dip, 2dip, 25dip, 35dip)
    Form1.RootPane.AddNode(sp1, 35dip, 2dip, 25dip, 35dip)
    Form1.RootPane.AddNode(sp2, 60dip, 2dip, 25dip, 35dip)
    Form1.RootPane.AddNode(sp3, 85dip, 2dip, 25dip, 35dip)
    Form1.RootPane.AddNode(sp4, 110dip, 2dip, 25dip, 35dip)
    Form1.RootPane.AddNode(sp5, 135dip, 2dip, 25dip, 35dip)
    Form1.RootPane.AddNode(sp6, 160dip, 2dip, 25dip, 35dip)
    Form1.RootPane.AddNode(sp7, 185dip, 2dip, 25dip, 35dip)
    Form1.RootPane.AddNode(sp8, 210dip, 2dip, 25dip, 35dip)
    Form1.RootPane.AddNode(sp9, 235dip, 2dip, 25dip, 35dip)
    Form1.RootPane.AddNode(sp10, 260dip, 2dip, 25dip, 35dip)
    Form1.RootPane.AddNode(sp11, 285dip, 2dip, 25dip, 35dip)
    Form1.RootPane.AddNode(sp12, 310dip, 2dip, 25dip, 35dip)
    Form1.RootPane.AddNode(sp13, 335dip, 2dip, 25dip, 35dip)
    Form1.RootPane.AddNode(sp14, 360dip, 2dip, 25dip, 35dip)
    Form1.RootPane.AddNode(sp15, 385dip, 2dip, 25dip, 35dip)
    
    sp0.RunMethod("setValue", Array(m.get("A")))
    sp1.RunMethod("setValue", Array(m.Get("B")))
    sp2.RunMethod("setValue", Array(m.Get("C")))
    sp3.RunMethod("setValue", Array(m.Get("D")))
    sp4.RunMethod("setValue", Array(m.Get("E")))
    sp5.RunMethod("setValue", Array(m.Get("F")))
    sp6.RunMethod("setValue", Array(m.Get("G")))
    sp7.RunMethod("setValue", Array(m.Get("X")))
    sp8.RunMethod("setValue", Array(m.Get("X")))
    sp9.RunMethod("setValue", Array(m.Get("T")))
    sp10.RunMethod("setValue", Array(m.Get("U")))
    sp11.RunMethod("setValue", Array(m.Get("V")))
    sp12.RunMethod("setValue", Array(m.Get("W")))
    sp13.RunMethod("setValue", Array(m.Get("X")))
    sp14.RunMethod("setValue", Array(m.Get("Y")))
    sp15.RunMethod("setValue", Array(m.Get("Z")))
    
    Sleep(1000)
    addListeners
    
End Sub

Sub sp0_Event (Methodname As String, Args As Object) As Object

    Try
        If m.Get(mysting.SubString2(0,1)) < Round(sp0.RunMethod("getValue", Null)) Then
            sp0.RunMethod("spinDown", Null)
        Else if m.Get(mysting.SubString2(0,1)) > Round(sp0.RunMethod("getValue", Null)) Then
            sp0.RunMethod("spinUp", Null)
        End If
        
    Catch
        Log(LastException)
    End Try
    Return 1
    
End Sub

Sub sp1_Event (Methodname As String, Args As Object) As Object
    Try
        If m.Get(mysting.SubString2(1,2)) < Round(sp1.RunMethod("getValue", Null)) Then
            sp1.RunMethod("spinDown", Null)
        Else if m.Get(mysting.SubString2(1,2)) > Round(sp1.RunMethod("getValue", Null)) Then
            sp1.RunMethod("spinUp", Null)
            
        End If
        
    
    Catch
        Log(LastException)
    End Try
    Return 1
End Sub


Sub sp2_Event (Methodname As String, Args As Object) As Object
    Try
        If m.Get(mysting.SubString2(2,3)) < Round(sp2.RunMethod("getValue", Null)) Then
            sp2.RunMethod("spinDown", Null)
        Else if m.Get(mysting.SubString2(2,3)) > Round(sp2.RunMethod("getValue", Null)) Then
            sp2.RunMethod("spinUp", Null)

        End If
        
    
    Catch
        Log(LastException)
    End Try
    Return 1
End Sub

Sub sp3_Event (Methodname As String, Args As Object) As Object
    
    Try
        If m.Get(mysting.SubString2(3,4)) < Round(sp3.RunMethod("getValue", Null)) Then
            sp3.RunMethod("spinDown", Null)
        Else if m.Get(mysting.SubString2(3,4)) > Round(sp3.RunMethod("getValue", Null)) Then
            sp3.RunMethod("spinUp", Null)

        End If
        
    
    Catch
        Log(LastException)
    End Try   
    Return 1
End Sub

Sub sp4_Event (Methodname As String, Args As Object) As Object
    Try
        If m.Get(mysting.SubString2(4,5)) < Round(sp4.RunMethod("getValue", Null)) Then
            sp4.RunMethod("spinDown", Null)
        Else if m.Get(mysting.SubString2(4,5)) > Round(sp4.RunMethod("getValue", Null)) Then
            sp4.RunMethod("spinUp", Null)

        End If
        
    Catch
        Log(LastException)
    End Try
    Return 1
End Sub

Sub sp5_Event (Methodname As String, Args As Object) As Object
    Try
        If m.Get(mysting.SubString2(5,6)) < Round(sp5.RunMethod("getValue", Null)) Then
            sp5.RunMethod("spinDown", Null)
        Else If m.Get(mysting.SubString2(5,6)) > Round(sp5.RunMethod("getValue", Null)) Then
            sp5.RunMethod("spinUp", Null)

        End If
    
    
    Catch
        Log(LastException)
    End Try
    Return 1
End Sub

Sub sp6_Event (Methodname As String, Args As Object) As Object
    Try
        If  m.Get(mysting.SubString2(6,7)) < Round(sp6.RunMethod("getValue", Null)) Then
            sp6.RunMethod("spinDown", Null)
        Else If m.Get(mysting.SubString2(6,7)) > Round(sp6.RunMethod("getValue", Null)) Then
            sp6.RunMethod("spinUp", Null)

        End If
    
    
    Catch
        Log(LastException)
    End Try
    Return 1
End Sub

Sub sp7_Event (Methodname As String, Args As Object) As Object
    Try
        If m.Get(mysting.SubString2(7,8)) < Round(sp7.RunMethod("getValue", Null)) Then
            sp7.RunMethod("spinDown", Null)
        Else If m.Get(mysting.SubString2(7,8)) > Round(sp7.RunMethod("getValue", Null)) Then
            sp7.RunMethod("spinUp", Null)

        End If
    
    
    Catch
        Log(LastException)
    End Try
    Return 1
End Sub

Sub sp8_Event (Methodname As String, Args As Object) As Object
    Try
        If m.Get(mysting.SubString2(8,9)) < Round(sp8.RunMethod("getValue", Null)) Then
            sp8.RunMethod("spinDown", Null)
        Else If m.Get(mysting.SubString2(8,9)) > Round(sp8.RunMethod("getValue", Null)) Then
            sp8.RunMethod("spinUp", Null)

        End If
    
    
    Catch
        Log(LastException)
    End Try
    Return 1
End Sub


Sub sp9_Event (Methodname As String, Args As Object) As Object
    Try
        If m.Get(mysting.SubString2(9,10)) < Round(sp9.RunMethod("getValue", Null)) Then
            sp9.RunMethod("spinDown", Null)
        Else If m.Get(mysting.SubString2(9,10)) > Round(sp9.RunMethod("getValue", Null)) Then
            sp9.RunMethod("spinUp", Null)

        End If
    
    
    Catch
        Log(LastException)
    End Try
    Return 1
End Sub

Sub sp10_Event (Methodname As String, Args As Object) As Object
    Try
        If m.Get(mysting.SubString2(10,11)) < Round(sp10.RunMethod("getValue", Null)) Then
            sp10.RunMethod("spinDown", Null)
        Else If m.Get(mysting.SubString2(10,11)) > Round(sp10.RunMethod("getValue", Null)) Then
            sp10.RunMethod("spinUp", Null)

        End If
    
    
    Catch
        Log(LastException)
    End Try
    Return 1
End Sub

Sub sp11_Event (Methodname As String, Args As Object) As Object
    Try
        If m.Get(mysting.SubString2(11,12)) < Round(sp11.RunMethod("getValue", Null)) Then
            sp11.RunMethod("spinDown", Null)
        Else If m.Get(mysting.SubString2(11,12)) > Round(sp11.RunMethod("getValue", Null)) Then
            sp11.RunMethod("spinUp", Null)

        End If
    
    
    Catch
        Log(LastException)
    End Try
    Return 1
End Sub

Sub sp12_Event (Methodname As String, Args As Object) As Object
    Try
        If m.Get(mysting.SubString2(12,13)) < Round(sp12.RunMethod("getValue", Null)) Then
            sp12.RunMethod("spinDown", Null)
            Else If m.Get(mysting.SubString2(12,13)) > Round(sp12.RunMethod("getValue", Null)) Then
            sp12.RunMethod("spinUp", Null)

        End If
    
    
    Catch
        Log(LastException)
    End Try
    Return 1
End Sub

Sub sp13_Event (Methodname As String, Args As Object) As Object
    Try
        If m.Get(mysting.SubString2(13,14)) < Round(sp13.RunMethod("getValue", Null)) Then
            sp13.RunMethod("spinDown", Null)
        Else If m.Get(mysting.SubString2(13,14)) > Round(sp13.RunMethod("getValue", Null)) Then
            sp13.RunMethod("spinUp", Null)

        End If
    
    
    Catch
        Log(LastException)
    End Try
    Return 1
End Sub

Sub sp14_Event (Methodname As String, Args As Object) As Object
    Try
        If m.Get(mysting.SubString2(14,15)) < Round(sp14.RunMethod("getValue", Null)) Then
            sp14.RunMethod("spinDown", Null)
        Else If m.Get(mysting.SubString2(14,15)) > Round(sp14.RunMethod("getValue", Null)) Then
            sp14.RunMethod("spinUp", Null)

        End If
    
    
    Catch
        Log(LastException)
    End Try
    Return 1
End Sub

Sub sp15_Event (Methodname As String, Args As Object) As Object
    Try
        If m.Get(mysting.SubString(15)) < Round(sp15.RunMethod("getValue", Null)) Then
            sp15.RunMethod("spinDown", Null)
        Else if m.Get(mysting.SubString(15)) > Round(sp15.RunMethod("getValue", Null)) Then
            sp15.RunMethod("spinUp", Null)

        End If
    
    
    Catch
        Log(LastException)
    End Try
    Return 1
End Sub





Sub addListeners
    
    Dim jo As JavaObject = sp0
    Dim e As Object = jo.CreateEvent("eu.hansolo.fx.spinner.event.SpinnerObserver", "sp0", False)
    sp0.RunMethod("setOnValueChanged", Array(e))
    
    Dim jo As JavaObject = sp1
    Dim e As Object = jo.CreateEvent("eu.hansolo.fx.spinner.event.SpinnerObserver", "sp1", False)
    sp1.RunMethod("setOnValueChanged", Array(e))
    
    Dim jo As JavaObject = sp2
    Dim e As Object = jo.CreateEvent("eu.hansolo.fx.spinner.event.SpinnerObserver", "sp2", False)
    sp2.RunMethod("setOnValueChanged", Array(e))
    
    Dim jo As JavaObject = sp3
    Dim e As Object = jo.CreateEvent("eu.hansolo.fx.spinner.event.SpinnerObserver", "sp3", False)
    sp3.RunMethod("setOnValueChanged", Array(e))
    
    Dim jo As JavaObject = sp4
    Dim e As Object = jo.CreateEvent("eu.hansolo.fx.spinner.event.SpinnerObserver", "sp4", False)
    sp4.RunMethod("setOnValueChanged", Array(e))
    
    Dim jo As JavaObject = sp5
    Dim e As Object = jo.CreateEvent("eu.hansolo.fx.spinner.event.SpinnerObserver", "sp5", False)
    sp5.RunMethod("setOnValueChanged", Array(e))
    
    Dim jo As JavaObject = sp6
    Dim e As Object = jo.CreateEvent("eu.hansolo.fx.spinner.event.SpinnerObserver", "sp6", False)
    sp6.RunMethod("setOnValueChanged", Array(e))
    
    Dim jo As JavaObject = sp7
    Dim e As Object = jo.CreateEvent("eu.hansolo.fx.spinner.event.SpinnerObserver", "sp7", False)
    sp7.RunMethod("setOnValueChanged", Array(e))
    
    Dim jo As JavaObject = sp8
    Dim e As Object = jo.CreateEvent("eu.hansolo.fx.spinner.event.SpinnerObserver", "sp8", False)
    sp8.RunMethod("setOnValueChanged", Array(e))
    
    Dim jo As JavaObject = sp9
    Dim e As Object = jo.CreateEvent("eu.hansolo.fx.spinner.event.SpinnerObserver", "sp9", False)
    sp9.RunMethod("setOnValueChanged", Array(e))
    
    Dim jo As JavaObject = sp10
    Dim e As Object = jo.CreateEvent("eu.hansolo.fx.spinner.event.SpinnerObserver", "sp10", False)
    sp10.RunMethod("setOnValueChanged", Array(e))
    
    Dim jo As JavaObject = sp11
    Dim e As Object = jo.CreateEvent("eu.hansolo.fx.spinner.event.SpinnerObserver", "sp11", False)
    sp11.RunMethod("setOnValueChanged", Array(e))
    
    Dim jo As JavaObject = sp12
    Dim e As Object = jo.CreateEvent("eu.hansolo.fx.spinner.event.SpinnerObserver", "sp12", False)
    sp12.RunMethod("setOnValueChanged", Array(e))
    
    Dim jo As JavaObject = sp13
    Dim e As Object = jo.CreateEvent("eu.hansolo.fx.spinner.event.SpinnerObserver", "sp13", False)
    sp13.RunMethod("setOnValueChanged", Array(e))
    
    Dim jo As JavaObject = sp14
    Dim e As Object = jo.CreateEvent("eu.hansolo.fx.spinner.event.SpinnerObserver", "sp14", False)
    sp14.RunMethod("setOnValueChanged", Array(e))
    
    Dim jo As JavaObject = sp15
    Dim e As Object = jo.CreateEvent("eu.hansolo.fx.spinner.event.SpinnerObserver", "sp15", False)
    sp15.RunMethod("setOnValueChanged", Array(e))
    
End Sub
 

Attachments

  • OdoMeterJHS.jar
    48.9 KB · Views: 8
  • b4jSpinner.zip
    4.1 KB · Views: 8

Johan Schoeman

Expert
Licensed User
Longtime User
If you want to see what is hiding in Args when the event(s) is/are raised (spx_Event(Methodname As String, Args As Object) As Object:

B4X:
Sub sp0_Event (Methodname As String, Args As Object) As Object
    
    If Methodname <> "hashCode" Then
    Dim aa(1) As Object = Args
    Log(aa(0))
    Dim bb As JavaObject = aa(0)
    Log(bb)
    Log(bb.RunMethod("getType", Null))
    Dim cc As JavaObject = bb.RunMethod("getSource", Null)
    Log(cc)
    Log(cc.RunMethod("getOldValue", Null))
    Log(cc.RunMethod("getSpinnerType", Null))
    
    
    End If
    Try
        If m.Get(mysting.SubString2(0,1)) < Round(sp0.RunMethod("getValue", Null)) Then
            sp0.RunMethod("spinDown", Null)
        Else if m.Get(mysting.SubString2(0,1)) > Round(sp0.RunMethod("getValue", Null)) Then
            sp0.RunMethod("spinUp", Null)
        End If
        
    Catch
        Log(LastException)
    End Try
    Return 1
    
End Sub

B4J Log:
eu.hansolo.fx.spinner.event.SpinnerEvent@764b8d7a
(SpinnerEvent) eu.hansolo.fx.spinner.event.SpinnerEvent@764b8d7a
VALUE_CHANGED
(CanvasSpinner) CanvasSpinner@2c20bc42[styleClass=spinner]
14.985666666666667
ALPHABETIC
 

Johan Schoeman

Expert
Licensed User
Longtime User
Attached is an example of how to change the spinner with a TextField and a Button. You can type in a space as a normal SPACE using your keyboard spacebar. All lower case will be converted to upper case.

Change the text in the TextField and click on button Go! Don't enter more than 16 chars unless you modify the code to accommodate more than 16 chars.

It only allows for A - Z and SPACE (" ") as it is in the original code unless selecting some spinners to be NUMERIC - Check the JAR in Post #1

1758445260499.png
 

Attachments

  • b4jSpinner.zip
    5 KB · Views: 1
Top