B4J Code Snippet Slider show value in thumb

Hi,

a neat little snippet showing the slider value while moving. Makes use of the JavaObject libary.

upload_2016-10-23_17-28-57.png


B4X:
Private Slider1 As Slider

Set the track color and bind a label to the slider.
B4X:
SliderTrackColorRGB(Slider1,0,0,255)
SliderBindLabel(Slider1, "%.0f")

Bind a label to a slider. The label displays the slider value.
B4X:
Sub SliderBindLabel(sldr As Slider, fmt As String)
    Dim lblvalue As Label
    lblvalue.Initialize("lblvalue")
    Dim joSlider As JavaObject = sldr
    Dim joLabel As JavaObject = lblvalue
    ' Get the valueproperty of the slider as StringBinding
    Dim vp As JavaObject = joSlider.RunMethodJO("valueProperty", Null).RunMethod("asString", Array(fmt))
    ' 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(lblvalue))
End Sub

' Set the color of the slider track using RGB colors, e.g. red=255,0,0, green=0,255,0,blue=0,0,255.
B4X:
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});"$))
End Sub
 

Attachments

  • upload_2016-10-23_17-24-44.png
    upload_2016-10-23_17-24-44.png
    3.3 KB · Views: 405

alienhunter

Active Member
Licensed User
Longtime User
Hi ,
thanks for sharing this
it does not display 1.5 how would i do this ?
thanks AH
slider.jpg
 
Last edited:

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

set the properties - either in the designer or in the code.
B4X:
Slider.ShowTickLabels = True
Slider.ShowTickMarks = True
 

rwblinn

Well-Known Member
Licensed User
Longtime User
OK - sorry = the answer was related to the CustomView library jRLViews.

For use with the standard B4J Slider, make use of a JavaObject to access additional properties.
Example:
B4X:
Private Slider1 As Slider

'Assign a JavaObject as Slider
Dim joSlider As JavaObject = Slider1
'Set additional properties which are not provided by the B4J Visual Designer
joSlider.RunMethod("setBlockIncrement", Array(10.0))    ' Must be double
joSlider.RunMethod("setMajorTickUnit", Array(10.0))     ' Must be double             
joSlider.RunMethod("setMinorTickCount", Array(20))      ' Must be Int
joSlider.RunMethod("setShowTickLabels", Array(True))
joSlider.RunMethod("setShowTickMarks", Array(True))
joSlider.RunMethod("setValueChanging", Array(True))
joSlider.RunMethod("setSnapToTicks", Array(True))
 
Last edited:
Top