Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

New Contributor
Posts: 7
Registered: ‎04-15-2010
My Device: 9530
My Carrier: simulater

Scrollbar Component in Blackberry



I am developing a Blackberry application, In that I need a vertical scrollbar.


Always vertical scrollbar should follow the cursor.




Always vertical scrollbar should be at the bottom of the screen.


How should we achive this. Presently I am using with LWUIT for this.


Please help me on this


Thanks in advance

BlackBerry Development Advisor
Posts: 15,723
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Scrollbar Component in Blackberry

You can use a VerticalFieldManager with scrolling enabled (styles VerticalFieldManager.VERTICAL_SCROLLBAR and VerticalFieldManager.VERTICAL_SCROLL).

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Scrollbar Component in Blackberry

As MSohm pointed out, VERTICAL_SCROLL and VERTICAL_SCROLLBAR provide some basic functionality.  If you create a VerticalFieldManager with those two styles it will display a small triangle in the upper right corner if there is more content above the visible area and a small triangle in the lower right corner if there's more content below it.


If you want to do anything fancier, though, you'll have to override the VerticalFieldManager's paint() and possibly sublayout().


1.  Choose an area on the right (left?) where you would like to display the slider.  I use a few private final static int constants like: SCROLLBAR_WIDTH, SCROLLBAR_RIGHT_MARGIN (if you don't want it to be hugging the right side of the VerticalFieldManager) as well as SCROLLBAR_COLOR and SCROLLBAR_SLIDER_COLOR.  Hope the names are self-explanatory.Smiley Wink

2.  Calculate the height of the slider (better do it once in sublayout() like this):


private int sliderHeight;
protected void sublayout(int w, int h) {
    super.sublaout(w, h);
    int fullHeight = getVirtualHeight();
    int visibleHeight = getVisibleHeight();
    sliderHeight = visibleHeight * visibleHeight / fullHeight;
    if (sliderHeight == 0) {
        sliderHeight = 1;  // would like to make it visible!



3. In paint(), add painting of the slider:


protected void paint(Graphics g) {
    int scrollPosition = getVerticalScroll();
    int scrollbarX = getVisibleWidth() - SCROLLBAR_WIDTH - SCROLLBAR_RIGHT_MARGIN;
    int scrollbarHeight = getVisibleHeight();
    int sliderPosition = (scrollPosition * scrollbarHeight) / getVirtualHeight();
    // Draw scrollbar "background"
    g.fillRect(scrollbarX, scrollPosition, SCROLLBAR_WIDTH + SCROLLBAR_RIGHT_MARGIN, scrollbarHeight);
    // Draw "slider"
    g.fillRect(scrollbarX, scrollPosition + sliderPosition,
               SCROLLBAR_WIDTH, sliderHeight);

 If you ever manually set vertical scroll with VerticalFieldManager.setVerticalScroll(), don't forget to invalidate()!





please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!