B4J Code Snippet Scrollbar Size

SubName: SetScrollbarSize

Sometimes the scrollbar is too wide, especially for small listviews, this code allows changing the width (for Vertical scrollbars) or height (for Horizontal scrollbars)

If you call this from a sub in which you load the layout - i.e. AppStart, DesignerCreateView etc., put it at the end of the sub and add a Sleep(0) command before it.

'Parent - The Node that ontains a scrollbar i.e. ListView, TableView etc.
'Orientation - can be VERTICAL, HORIZONTAL or BOTH
'Size - The required width for a VERTICAl scrollbar or height for a HORIZONTAL scroll bar
Public Sub SetScrollbarSize(Parent As JavaObject, Orientation As String, Size As Double)
    'Get a Set that contains the scrollbars attached to the parent and convert it to an array
    Dim Arr() As Object = Parent.RunMethodJO("lookupAll",Array(".scroll-bar")).RunMethod("toArray",Null)

    For Each N As Node In Arr

        'Check this object is a scrolbar
        If GetType(N) = "com.sun.javafx.scene.control.skin.VirtualScrollBar" Or GetType(N) = "javafx.scene.control.ScrollBar" Then
            Dim SB As JavaObject = N

            'Get the orientation of the scrollbar as a string
            Dim BarOrientation As String = SB.RunMethodJO("getOrientation",Null).RunMethod("toString",Null)

            'Required Orientation is VERTICAL or BOTH
            If BarOrientation = "VERTICAL" And (Orientation  = BarOrientation Or Orientation = "BOTH") Then
                N.PrefWidth = Size
            End If

            'Required Orientation is HORIZONTAL or BOTH
            If BarOrientation = "HORIZONTAL" And (Orientation = BarOrientation Or Orientation = "BOTH") Then
                N.PrefHeight = Size
            End If
        End If
End Sub

Dependencies: JavaObject

Tags: B4J Scrollbar size width height
Last edited:


Licensed User
Updated to include Textarea scrollbars