B4J Question Make native Slider "stepable" ?

GMan

Well-Known Member
Licensed User
Longtime User
Is it possible to make the Slider stepable ?
Now, when i slide, even the numbers behind the "." are show - but i want also numbers at int, so max. 3 Digits, no "." etc.
1792956301832.png
 
With JavaObject:

Code:
' Set option block increment - Must be double.
' Only works with keyboard arrow keys.
' Example:
' Private SliderHorizontal As Slider
' SetSliderBlockIncrement(SliderHorizontal, 10.0)
Private Sub SetSliderBlockIncrement(sldr As Slider, Value As Double)
    Dim joSldr As JavaObject = sldr
    joSldr.RunMethod("setBlockIncrement", Array(Value))
End Sub

Tested with B4J 10.70, JDK 19.

Here some more examples using JavaObject:

B4X:
Functions for the JavaFX Slider.
These functions use in most cases the JavaFX Slider view as argument for use in other applications.
The JavaObject library is used to run API functions for the slider.

'Set the value of the slider.
'sldr (slider) - Slider created as view
'value (double) - New slider value. Must be in min/max range.
Private Sub SetSliderValue(sldr As Slider, value As Double)
    Dim newvalue As Double = Max(sldr.MinValue, Min(sldr.MaxValue, value))
    sldr.Value = newvalue
End Sub

'Set the slider orientation horizontal or vertical.
'slr - Slider created as view
'orientation - string HORIZONTAL or VERTICAL
Private Sub SetSliderOrientation(sldr As Slider, orientation As String)
    Dim joSldr As JavaObject = sldr

    'Create a static instance for the enum orientation HORIZONTAL, VERTICAL
    Dim joEnum As JavaObject
    Dim joOrientation As JavaObject = joEnum.InitializeStatic("javafx.geometry.Orientation")

    'Run the method valueOf for the orientation string like "HORIZONTAL" or "VERTICAL"
    joSldr.RunMethod("setOrientation", Array(joOrientation.RunMethod("valueOf", Array(orientation.ToUpperCase))))
End Sub

'Set option block increment - Must be double.
Private Sub SetSliderBlockIncrement(sldr As Slider, Value As Double)
    Dim joSldr As JavaObject = sldr
    joSldr.RunMethod("setBlockIncrement", Array(Value))
End Sub

'Set option major tick unit - Must be double.
Private Sub SetSliderMajorTickUnit(sldr As Slider, Value As Double)
    Dim joSldr As JavaObject = sldr
    joSldr.RunMethod("setMajorTickUnit", Array(Value))
End Sub

'Set option minor tick count - Must be int.
Private Sub SetSliderMinorTickCount(sldr As Slider, Value As Int)
    Dim joSldr As JavaObject = sldr
    joSldr.RunMethod("setMinorTickCount", Array(Value))
End Sub

'Set option to show the tick labels.
Private Sub SetSliderShowTickLabels(sldr As Slider, State As Boolean)
    Dim joSldr As JavaObject = sldr
    joSldr.RunMethod("setShowTickLabels", Array(State))
End Sub

'Set option to show the  tick marks.
Private Sub SetSliderShowTickMarks(sldr As Slider, State As Boolean)
    Dim joSldr As JavaObject = sldr
    joSldr.RunMethod("setShowTickMarks", Array(State))
End Sub

'Set option to snap the value to the ticks.
Private Sub SetSliderSnapToTicks(sldr As Slider, State As Boolean)
    Dim joSldr As JavaObject = sldr
    joSldr.RunMethod("setSnapToTicks", Array(State))
End Sub

'Set the thumb as a circle with radius by using the font-size style.
'Example: SetSliderThumbCircle(SliderControl, 18, xui.color_green)
Private Sub SetSliderThumbCircle(sldr As Slider, radius As Int, color As Int)
    Dim joSlr As JavaObject = sldr
    Dim c() As Int = ColorXUIToRGB(color)
    Dim style As String = $"-fx-background-color:rgb(${c(0)},${c(1)},${c(2)}); -fx-font-size: ${radius};"$
    joSlr.RunMethodJo("lookup", Array(".thumb")).RunMethod("setStyle", Array(style))
End Sub

'Set the thumb as a square. The backgroundradius is set to 0.
'Example: SetSliderThumbSquare(SliderControl, 18, xui.color_red)
Private Sub SetSliderThumbSquare(sldr As Slider, radius As Int, color As Int)
    Dim joSlr As JavaObject = sldr
    Dim c() As Int = ColorXUIToRGB(color)
    Dim backgroundradius As Int = 0
    Dim style As String = $"-fx-background-color:rgb(${c(0)},${c(1)},${c(2)}); -fx-background-radius: ${backgroundradius}; -fx-font-size: ${radius};"$
    joSlr.RunMethodJo("lookup", Array(".thumb")).RunMethod("setStyle", Array(style))
End Sub

'Set the thumb as a rectangle with size w * h and borderradius 1.
'Example: SetSliderThumbRectangle(SliderControl, mThumbRectangleWidth, mThumbRectangleHeight, mThumbColor)
Private Sub SetSliderThumbRectangle(sldr As Slider, width As Int, height As Int, rgb As Int)
    Dim joSlr As JavaObject = sldr
    Dim cornerradius As Int = 2
    If height == -1 Then height = width / 2
    Dim c() As Int = ColorXUIToRGB(rgb)
    Dim style As String = $"-fx-background-color:rgb(${c(0)},${c(1)},${c(2)}); -fx-pref-height:${height}; -fx-pref-width:${width}; -fx-background-radius:${cornerradius}em;"$
    joSlr.RunMethodJo("lookup", Array(".thumb")).RunMethod("setStyle", Array(style))
End Sub

'Set the thumb as a knob with size w * h and borderradius 1.
'The knob is an image stored in the files folder (dirassets).
'KnobOff has a red dot in the middle, KnobOn a green dot to indicate value > 0.
'Example: SetSliderThumbKnobOn(SliderControl)
Private Sub SetSliderThumbKnob(sldr As Slider, on As Boolean)
    If on Then
        SetSliderThumbIcon(sldr, "KnobOn.png")
    Else
        SetSliderThumbIcon(sldr, "KnobOff.png")
    End If
End Sub

' Set the icon of the slider thumb.
' The thumb background and border color are set to transparent. The icon must be located in the files folder (dirassets).
' Example: SetSliderThumbIcon(Slider1, "arrowdown-green.png")
Private Sub SetSliderThumbIcon(sldr As Slider, img As String)
    Dim joSlr As JavaObject = sldr
    Dim iv As ImageView
    iv.Initialize("")
    iv.SetImage(fx.LoadImage(File.DirAssets, img))
    Dim style As String = $"-fx-background-color:transparent;-fx-border-color:transparent;"$
    joSlr.RunMethodJo("lookup", Array(".thumb")).RunMethod("setStyle", Array(style))
    joSlr.RunMethodJo("lookup", Array(".thumb")).RunMethodJo("getChildren", Null).RunMethod("clear", Null)
    joSlr.RunMethodJo("lookup", Array(".thumb")).RunMethodJo("getChildren", Null).RunMethod("add", Array(iv))
End Sub

'Set the color of the slider thumb using RGB values 0-255.
'Example: setSliderThumbColorRGB(Slider1, 0,255,0) which sets the thumb color to green.
Private Sub SetSliderThumbColorRGB(sldr As Slider, r As Int, g As Int, b As Int)        'ignore
    Dim joSlr As JavaObject = sldr
    Dim style As String = $"-fx-background-color:rgb(${r},${g},${b});"$
    joSlr.RunMethodJo("lookup", Array(".thumb")).RunMethod("setStyle", Array(style))
End Sub

'Set the color of the slider thumb using xui color value (int).
'Example: setSliderThumbColorXUI(Slider1, xui.color_blue) which sets the thumb color to green.
Private Sub SetSliderThumbColorXUI(sldr As Slider, rgb As Int)
    Dim joSlr As JavaObject = sldr
    Dim c() As Int = ColorXUIToRGB(rgb)
    Dim style As String = $"-fx-background-color:rgb(${c(0)},${c(1)},${c(2)});"$
    joSlr.RunMethodJo("lookup", Array(".thumb")).RunMethod("setStyle", Array(style))
End Sub

'Set the color of the slider track by name, e.g. red, green, blue etc.
'Example: setSliderTrackColorName(Slider1, "blue")
Private Sub SetSliderTrackColorName(sldr As Slider, name As String)    'ignore
    Dim joSlr As JavaObject = sldr
    Dim style As String = $"-fx-background-color:${name};"$
    joSlr.RunMethodJo("lookup", Array(".track")).RunMethod("setStyle", Array(style))
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.
'Example: setSliderTrackColorRGB(Slider1, 0, 255, 0) sets the track color to green.
Private Sub SetSliderTrackColorRGB(sldr As Slider, r As Int, g As Int, b As Int)    'ignore
    Dim joSlr As JavaObject = sldr
    Dim style As String = $"-fx-background-color:rgb(${r},${g},${b});"$
    joSlr.RunMethodJo("lookup", Array(".track")).RunMethod("setStyle", Array(style))
End Sub

'Set the color of the slider track using the XUI color (int).
'Example: setSliderTrackColorXUI(Slider1, xui.color_red) sets the track color to red.
Private Sub SetSliderTrackColorXUI(sldr As Slider, color As Int)
    Dim joSlr As JavaObject = sldr
    Dim c() As Int = ColorXUIToRGB(color)
    Dim style As String = $"-fx-background-color:rgb(${c(0)},${c(1)},${c(2)});"$
    joSlr.RunMethodJo("lookup", Array(".track")).RunMethod("setStyle", Array(style))
End Sub

'Set color of the track showing a coloured range from 0 to value.
'The track background color is set for the track value range
'Example: SetSliderTrackValueColor(SliderControl, 101, 255, xui_color_black, xui.color_red)
Private Sub SetSliderTrackValueColor(sldr As Slider, orientation As String, value As Double, maxvalue As Double, trackbasecolor As Int, trackvaluecolor As Int)
    Dim joSlr As JavaObject = sldr

    'Define the trackstyle
    Dim style As String

    'Get the colors as hex string RRGGBB
    Dim trackbasecolorhex As String = ColorXUIToHexRGB(trackbasecolor)
    Dim trackvaluecolorhex As String = ColorXUIToHexRGB(trackvaluecolor)

    'Set the percentage of the track to set the value color
    Dim pct As Int = 100 - Round(100.0 * value / maxvalue)
    If orientation == "VERTICAL" Then
        'Vertical linear gradient with 2 tracks: base track color 0%-100% + value track color 0%-value%
        style = $"-fx-background-color: linear-gradient(#${trackbasecolorhex} ${pct}%, #${trackvaluecolorhex} ${pct}%);"$
    Else
        'Horizontal linear gradient with 2 tracks: track base color 0%-value% + value track color value%-100%
        style = $"-fx-background-color: linear-gradient(from 100% 0% to 0% 100%, #${trackbasecolorhex} 0%, #${trackbasecolorhex} ${pct}%, #${trackvaluecolorhex} ${pct}%, #${trackvaluecolorhex} 100%);"$
    End If

    joSlr.RunMethodJo("lookup", Array(".track")).RunMethod("setStyle", Array(style))
End Sub

'Set the tick label font family, size and color.
'fontstyle (font) - Font style inluding size. Must be created if not exists.
'labelcolor (int) - Color of the label tick and text. Ensure to convert from paint in case designer label property is used.
'Example:
'SetTickLabelFontAndColor(SliderControl, mLbl.Font, xui.PaintOrColorToColor(mLbl.TextColor))
'or from external
'Private mTickLabelFont as Font
'mTickLabelFont = fx.LoadFont(File.DirAssets, $"Digital.ttf"$, mLbl.TextSize)
'SetSliderTickLabelFontAndColor(SliderControl, mTickLabelFont , xui.color_blue)
Private Sub SetSliderTickLabelFontAndColor(sldr As Slider, fontstyle As Font, color As Int)
    Dim joSlr As JavaObject = sldr

    Dim joNumberAxis As JavaObject = joSlr.RunMethodJo("lookup", Array(".axis"))
    joNumberAxis.RunMethod("setTickLabelFont", Array(fontstyle))
    joNumberAxis.RunMethod("setTickLabelFill", Array(fx.Colors.From32Bit(color)))
End Sub

'Set the tick label font family, size and color.
'family (string) - Font family name, like System
'size (int) - Font size
'color (int) - Color of the label tick and text. Ensure to convert from paint in case designer label property is used.
'Example:
'SetSliderTickLabelFontFamilyAndColor(SliderControl, "System", 24, xui.color_blue)
Private Sub SetSliderTickLabelFontFamilyAndColor(sldr As Slider, family As String, size As Int, color As Int)    'ignore
    Dim joSlr As JavaObject = sldr
    Dim colorhex As String = ColorXUIToHexRGB(color)
    Dim style As String = $"-fx-tick-label-font-size:${size}px;-fx-tick-label-font-family:${family};-fx-tick-label-fill:#${colorhex}"$
    joSlr.RunMethodJo("lookup", Array(".axis")).RunMethod("setStyle", Array(style))
End Sub

'Set the text of the slider label.
'If orientation is vertical, then text is shown at the right 90° centered.
'If orientation is horizontal, then text is shown at the bottom centered.
'Example: SetSliderAxisLabel(SliderControl, "This is a slider")
'TODO: How to change the default font style for a label axis
Private Sub SetSliderAxisLabel(sldr As Slider, text As String)
    Dim joSlr As JavaObject = sldr
    Dim joNumberAxis As JavaObject = joSlr.RunMethodJo("lookup", Array(".axis"))
    joNumberAxis.RunMethod("setLabel", Array(text))
End Sub

'Bind a label to a slider. The label displays the slider value.
'The label embedded in the thumb is created and the font is set
'Example: SliderBindLabel(Slider1, "%.1f")
Private Sub SetSliderBindLabel(sldr As Slider, lbl As Label, valuefmt As String)
    Dim joSlider As JavaObject = sldr
    Dim lblvalue As Label

    'Init the label
    lblvalue.Initialize("lblvalue")
    'Assign the argument lbl to the font and texsize
    lblvalue.Font = lbl.Font
    lblvalue.TextSize = lbl.TextSize
    lblvalue.TextColor = lbl.TextColor

    'Then assign the label to the javaobject label
    Dim joLabel As JavaObject = lblvalue

    ' Get the valueproperty of the slider as StringBinding
    Dim vp As JavaObject = joSlider.RunMethodJO("valueProperty", Null).RunMethod("asString", Array(valuefmt))

    ' 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
#End Region

#Region Events
'Event: SliderMouseReleased_Event
'Handle the slider mousereleased event.
'This event happens prior SliderControl_ValueChange. To ensure the right slider value is set, a global flag is used.
'This flag is handled in SliderControl_ValueChange.
'The slider value can be adjusted if snaptoticks is used. For example moving the slider to 36 will snap to ticks 50, which is the new slider value handled by slider_valuechange event.
'The mouse event API method getSource is used to get the slider clicked. The event first argument is used for that.
Private Sub SliderMouseReleased_Event(EventName As String, Args() As Object) As Object
    Log($"[SliderMouseReleased_Event]value=${mValue},eventname=${EventName},args=${Args}"$)
    mSliderMouseReleased = False
    'Only use if the valuechanging property is not used (set in designer).
    If Not(mValueChanging) Then
        If Args.length > 0 Then
            mSliderMouseReleased = True
        End If
    End If
    Return False
End Sub

'Callback: Slider value change called by event Slider ValueChange.
Private Sub SliderControl_Callback
    Log($"[SliderControl_Callback]value=${mValue},callback=${mCallBack},event=${mEventName}"$)
    'Check if the callback sub exists on the callback
    If xui.SubExists(mCallBack, mEventName & "_ValueChange", 1) Then
        CallSubDelayed2(mCallBack, mEventName & "_ValueChange", mValue)
    End If
End Sub

'Event: Slider value change on every mouse or touch move.
'The global mValue is set and the UI views are updated.
'value (double) - slider value. Note of snaptoticks is set, the value is adjusted to a tick
Private Sub SliderControl_ValueChange(value As Double)
    Log($"[SliderControl_ValueChange]value=${mValue}/${value}"$)

    'Assign the value to the global
    mValue = value

    'Check if the value should be rounded if digits property is 0.
    If mDigits == 0 Then mValue = Round(mValue)

    'Slider Knob change bitmap to on <> off depending value (0=off)
    If mThumbType == "Knob" Then
        SetSliderThumbKnob(SliderControl, IIf(mValue > 0, True, False))
    End If

    'Set the slider track range from minvalue to selected value
    SetSliderTrackValueColor(SliderControl, mOrientation, mValue, mMaxValue, mTrackBaseColor, mTrackValueColor)

    'Invoke the callback event depending property ValueChanging
    If mValueChanging Then
        SliderControl_Callback
        Return
    End If

    'Handle special case in case mouse is released
    If Not(mValueChanging) And mSliderMouseReleased Then
        SliderControl_Callback
    End If
End Sub
 
Upvote 0
This is awesome...but dont work.
Throws no error, but has no effect.

I am using the standard slider from the core.

In AppStart:
Code:
    SetSliderBlockIncrement(SliderTest, 20.0)

then:

B4X:
Private Sub SliderTest_ValueChange (Value As Double)
    LabelTest.Text.IndexOf2(".",1)
    LabelTest.Text = SliderTest.Value
End Sub
Private Sub SetSliderBlockIncrement(sldr As Slider, Value As Double)
    Dim joSldr As JavaObject = sldr
    joSldr.RunMethod("setBlockIncrement", Array(Value))
End Sub

READ IT TO LATE (as many times):
Only for the keyboard - there it works.

Nice options though....most take effect, this one is missing ColorXUItoRGB

Code:
Private Sub SetSliderThumbCircle(sldr As Slider, radius As Int, color As Int)
    Dim joSlr As JavaObject = sldr
    Dim c() As Int = ColorXUIToRGB(color)
    Dim style As String = $"-fx-background-color:rgb(${c(0)},${c(1)},${c(2)}); -fx-font-size: ${radius};"$
    joSlr.RunMethodJo("lookup", Array(".thumb")).RunMethod("setStyle", Array(style))
End Sub
 
Last edited by a moderator:
Upvote 0
Back
Top