B4J Question Create Slider - JavaObject problem

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I am creating a slider in my code to a one of my TreeView Columns as it is being created.

B4X:
...
               Dim TrackItem           As TreeTableItem

                Dim TrackLabel         As Button = CreateButton("TrackNumber")
                Dim MP3Slider        As Slider =   CreateSlider("MP3Slider")
                
                TrackItem.Initialize("Track", Array As Object("", Column, MP3Slider, "", TrackLabel, FormatTime(TrackInfo.Duration), TrackInfo.Song, TrackInfo.TrackArtist, TrackInfo.FileName, ""))
...


Public  Sub CreateSlider(InitAs As String) As Slider
            Dim MakeSlider As Slider
    
            MakeSlider.Initialize(InitAs)

'            SliderTrackColorRGB(MakeSlider, 0,    0,    255)            ' Either of these routines fail with JavaObject
'            SliderBindLabel(MakeSlider,     "%.0f")    

            Return MakeSlider

and trying to use the code show here https://www.b4x.com/android/forum/threads/slider-show-value-in-thumb.72271/#content

B4X:
Public  Sub SliderTrackColorRGB(slr As Slider, r As Int, g As Int, b As Int)
            Dim joSldr As JavaObject = slr
            joSldr.RunMethodJo("lookup", Array(".track")).RunMethod("setStyle", Array($"-fx-background-color:rgb(${r},${g},${b});"$))   [B][U][I]  '  This line fails[/I][/U][/B]
End Sub

Public  Sub SliderBindLabel(sldr As Slider, fmt As String)
            Dim SliderLabel As Label
            
            SliderLabel.Initialize("SliderLabel")
            
            Dim joSlider     As JavaObject     = sldr
            Dim joLabel     As JavaObject     = SliderLabel
            
            ' Get the valueproperty of the slider as StringBinding
            Dim vp As JavaObject = joSlider.RunMethodJO("valueProperty", Null).RunMethod("asString", Array(fmt)) [B][U][I] '  This line fails[/I][/U][/B]
            
            ' Bind the slider stringbinding to the label
            joLabel.RunMethodjo("textProperty", Null).runmethodjo("bind", Array(vp))
            
            ' Add the label to the slider thumb
            joSlider.RunMethodJo("lookup", Array(".thumb")).RunMethodJo("getChildren", Null).RunMethod("add", Array(SliderLabel))
End Sub
[/code

but the routines keep failing saying 
java.lang.RuntimeException: Object should first be initialized (JavaObject).

If I DO NOT call those routines (calling just to make slider look nice) the slider in the TreeView Column is created just fine and works.
 

stevel05

Expert
Licensed User
Longtime User
You probably need to give it some time to finish creating the view before you can run the lookup on it. Try putting a sleep statement after the initialize and before the call to your routines, maybe start with a largish value (1000) and see if works, then reduce it. Sleep(0) may be enough. Although it may depend on what else is running and whether the screen has been updated yet.
 
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I tried Sleep(0).

They are created as Invisible and not enabled. When I actually enable and display them I set them and that seems to work fine
 
Upvote 0
Top