Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8705837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:27:53+00:00 2026-06-13T03:27:53+00:00

I have created a SliderField class in order to show a drag-able slider in

  • 0

I have created a SliderField class in order to show a drag-able slider in my BlackBerry application. The slider is basically used to set interval for location updates (via GPS) in the app. The app is built on OS 5.0 and so no API was found for the SliderField. The problem
I am facing is that I need to increase the integer value in a label as the slider is moved. For instance, if the integer value in a label is 1 and the user moves the slider the value should increase to 5,10,15 etc. I do not know how to link moving the slider to increasing the value in the label field. Can anyone please help? I am adding the SliderField object to the screen as below:

Bitmap sliderBack = Bitmap.getBitmapResource( "progress51.png" );
    Bitmap sliderFocus = Bitmap.getBitmapResource( "progress51.png" );
    Bitmap sliderThumb = Bitmap.getBitmapResource( "butt.png" );
SliderField theSlider = new SliderField( sliderThumb, sliderBack, sliderFocus,20, 1, 1, 1 );
         secondHFM.add(theSlider)

Below is my code for the SliderField class.

public class SliderField extends Field
 {
 Bitmap _imageThumb;

Bitmap _imageSlider;
Bitmap _imageSliderLeft;
Bitmap _imageSliderCenter;
Bitmap _imageSliderRight;

Bitmap _imageSliderFocus;
Bitmap _imageSliderFocusLeft;
Bitmap _imageSliderFocusCenter;
Bitmap _imageSliderFocusRight;

private int _numStates;
private int _currentState;
private boolean _selected;

private int _xLeftBackMargin;
private int _xRightBackMargin;

private int _thumbWidth;
private int _thumbHeight;

private int _totalHeight;
private int _totalWidth;

private int _rop;

private int _backgroundColours[];
private int _backgroundSelectedColours[];

private int _defaultSelectColour = 0x977DED;
private int _defaultBackgroundColour = 0x000000;
private int _defaultHoverColour = 0x999999;


public SliderField( Bitmap thumb
            , Bitmap sliderBackground
            , int numStates
            , int initialState
            , int xLeftBackMargin
            , int xRightBackMargin )
{
    this( thumb, sliderBackground, sliderBackground, numStates, initialState, xLeftBackMargin, xRightBackMargin, FOCUSABLE );
}

public SliderField( Bitmap thumb
            , Bitmap sliderBackground
            , int numStates
            , int initialState
            , int xLeftBackMargin
            , int xRightBackMargin
            , long style )
{  
    this( thumb, sliderBackground, sliderBackground, numStates, initialState, xLeftBackMargin, xRightBackMargin, style );
}

public SliderField( Bitmap thumb
            , Bitmap sliderBackground
            , Bitmap sliderBackgroundFocus
            , int numStates
            , int initialState
            , int xLeftBackMargin
            , int xRightBackMargin )
{
    this( thumb, sliderBackground, sliderBackgroundFocus, numStates, initialState, xLeftBackMargin, xRightBackMargin, FOCUSABLE );
}

public SliderField( Bitmap thumb
            , Bitmap sliderBackground
            , Bitmap sliderBackgroundFocus
            , int numStates
            , int initialState
            , int xLeftBackMargin
            , int xRightBackMargin
            , long style )
{
    super( style );

    if( initialState > numStates || numStates < 2 ){
    }
    _imageThumb = thumb;
    _imageSlider = sliderBackground;
    _imageSliderFocus = sliderBackgroundFocus;
    _numStates = numStates;
    setState( initialState );

    _xLeftBackMargin = xLeftBackMargin;
    _xRightBackMargin = xRightBackMargin;

    _rop = _imageSlider.hasAlpha() ? Graphics.ROP_SRC_ALPHA : Graphics.ROP_SRC_COPY;

    _thumbWidth = thumb.getWidth();
    _thumbHeight = thumb.getHeight();
    initBitmaps();
}              
public SliderField( Bitmap thumb
            , Bitmap sliderBackground
            , int numStates
            , int initialState
            , int xLeftBackMargin
            , int xRightBackMargin
            , int[] colours
            , int[] selectColours )
{
    this(thumb, sliderBackground, sliderBackground, numStates, initialState, xLeftBackMargin, xRightBackMargin, FOCUSABLE );

    if( colours.length != numStates+1 ){
        throw new IllegalArgumentException();
    }
    _backgroundColours = colours;
    _backgroundSelectedColours = selectColours;
}
public void initBitmaps()
{
    int height = _imageSlider.getHeight();

    _imageSliderLeft = new Bitmap( _xLeftBackMargin, height );
    _imageSliderCenter = new Bitmap( _imageSlider.getWidth() - _xRightBackMargin - _xLeftBackMargin, height);
    _imageSliderRight = new Bitmap( _xRightBackMargin, height );

    copy( _imageSlider, 0, 0, _xLeftBackMargin, height, _imageSliderLeft );
    copy( _imageSlider, _xLeftBackMargin, 0, _imageSlider.getWidth() - _xRightBackMargin - _xLeftBackMargin, height, _imageSliderCenter);
    copy( _imageSlider, _imageSlider.getWidth() - _xRightBackMargin, 0, _xRightBackMargin, height, _imageSliderRight);

    _imageSliderFocusLeft = new Bitmap( _xLeftBackMargin, height );
    _imageSliderFocusCenter = new Bitmap( _imageSlider.getWidth() - _xRightBackMargin - _xLeftBackMargin, height);
    _imageSliderFocusRight = new Bitmap( _xRightBackMargin, height );

    copy( _imageSliderFocus, 0, 0, _xLeftBackMargin, height, _imageSliderFocusLeft );
    copy( _imageSliderFocus, _xLeftBackMargin, 0, _imageSlider.getWidth() - _xRightBackMargin - _xLeftBackMargin, height, _imageSliderFocusCenter);
    copy( _imageSliderFocus, _imageSlider.getWidth() - _xRightBackMargin, 0, _xRightBackMargin, height, _imageSliderFocusRight);
}
private void copy(Bitmap src, int x, int y, int width, int height, Bitmap dest) {
    int[] argbData = new int[width * height];
    src.getARGB(argbData, 0, width, x, y, width, height);
    for(int tx = 0; tx < dest.getWidth(); tx += width) {
        for(int ty = 0; ty < dest.getHeight(); ty += height) {
            dest.setARGB(argbData, 0, width, tx, ty, width, height);
        }
    }
}
public void setState(int newState) {
    if( newState > _numStates ){
        throw new IllegalArgumentException();
    } else {
        _currentState = newState;
        invalidate();
    }
}
public int getState() {
    return _currentState;
}
public int getNumStates() {
    return _numStates;
}
public int getColour() {
    if(_backgroundSelectedColours != null) {
        return _backgroundSelectedColours[getState()];
    }
    return 0x000000;
}

public int getPreferredWidth() {
    return _totalWidth;
}

public int getPreferredHeight() {
    return _totalHeight;
}
protected void layout( int width, int height ) {
    if (width < 0 || height < 0)
        throw new IllegalArgumentException();
    _totalWidth = width;
    _totalHeight = Math.max(_imageSlider.getHeight(), _imageThumb.getHeight());

    setExtent( _totalWidth, _totalHeight );
}

public void paint( Graphics g )
{
    int sliderHeight = _imageSlider.getHeight();
    int sliderBackYOffset = ( _totalHeight - sliderHeight ) >> 1;
    int backgroundColor = _defaultBackgroundColour;
    if( _backgroundSelectedColours != null || _backgroundColours != null ) {

        if( _selected ) {
            backgroundColor = _backgroundSelectedColours != null ? _backgroundSelectedColours[getState()] : _defaultSelectColour;
        } else if(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS)) {
            backgroundColor = _backgroundColours != null ? _backgroundColours[getState()] : _defaultHoverColour;
        } else {
            backgroundColor = _defaultBackgroundColour;
        }
    }
    g.setColor( backgroundColor );
    g.fillRect( 1, sliderBackYOffset + 1, _totalWidth - 2, sliderHeight - 2 );

    if(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS)) {    
        paintSliderBackground( g, _imageSliderFocusLeft, _imageSliderFocusCenter, _imageSliderFocusRight );
    } else {
        paintSliderBackground( g, _imageSliderLeft, _imageSliderCenter, _imageSliderRight );
    }
    int thumbXOffset = ( ( _totalWidth - _thumbWidth ) * _currentState ) / _numStates;
    g.drawBitmap( thumbXOffset, ( _totalHeight - _thumbHeight ) >> 1, _thumbWidth, _thumbHeight, _imageThumb, 0, 0 );
}

private void paintSliderBackground( Graphics g, Bitmap left, Bitmap middle, Bitmap right ) 
{
    int sliderHeight = _imageSlider.getHeight();
    int sliderBackYOffset = ( _totalHeight - sliderHeight ) >> 1;
    g.drawBitmap( 0, sliderBackYOffset, _xLeftBackMargin, sliderHeight, left, 0, 0 );
    g.tileRop( _rop, _xRightBackMargin, sliderBackYOffset, _totalWidth - _xLeftBackMargin - _xRightBackMargin, sliderHeight, middle, 0, 0 );
    g.drawBitmap( _totalWidth - _xRightBackMargin, sliderBackYOffset, _xRightBackMargin, sliderHeight, right, 0, 0 );
}

public void paintBackground( Graphics g ) 
{
}

protected void drawFocus( Graphics g, boolean on )
{
    boolean oldDrawStyleFocus = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS );
    try {
        if( on ) {
            g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, true );
        }
        paint( g );
    } finally {
        g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus );
    }
}
protected boolean touchEvent(TouchEvent message)
{
    boolean isConsumed = false;
    boolean isOutOfBounds = false;
    int x = message.getX(1);
    int y = message.getY(1);
    if(x < 0 || y < 0 || x > getExtent().width || y > getExtent().height) {
        isOutOfBounds = true;
    }
    switch(message.getEvent()) {
        case TouchEvent.CLICK:
        case TouchEvent.MOVE:
            if(isOutOfBounds) return true; 
            _selected = true; 

            int stateWidth = getExtent().width / _numStates;
            int numerator = x / stateWidth;
            int denominator = x % stateWidth;
            if( denominator > stateWidth / 2 ) {
                numerator++;
            }
            _currentState = numerator;
            invalidate();

            isConsumed = true;
            break;
        case TouchEvent.UNCLICK:
            if(isOutOfBounds) {
                _selected = false; 
                return true;
            }
            _selected = false; 
            stateWidth = getExtent().width / _numStates;
            numerator = x / stateWidth;
            denominator = x % stateWidth;
            if( denominator > stateWidth / 2 ) {
                numerator++;
            }
            _currentState = numerator;
            invalidate();
            fieldChangeNotify(0);
            isConsumed = true;
            break;
    }
    return isConsumed;
}
protected boolean navigationMovement(int dx, int dy, int status, int time) 
{
    if( _selected )
    {
        if(dx > 0 || dy > 0) {
            incrementState();
            fieldChangeNotify( 0 );
            return true;
        } else if(dx < 0 || dy < 0) {
            decrementState();
            fieldChangeNotify( 0 );
            return true;
        }
    }
    return super.navigationMovement( dx, dy, status, time);
}
public void decrementState() {
    if(_currentState > 0) {
        _currentState--;
        invalidate();
    }
}
public void incrementState() {
    if(_currentState < _numStates) {
        _currentState++;
        invalidate();
    }
}
protected boolean invokeAction(int action) {
    switch(action) {
        case ACTION_INVOKE: {
            toggleSelected();
            return true;
        }
    }
    return false;
}
protected boolean keyChar( char key, int status, int time ) {
    if( key == Characters.SPACE || key == Characters.ENTER ) {
        toggleSelected();
        return true;
    }

    return false;
}
protected boolean trackwheelClick( int status, int time ) {
    if( isEditable() ) {
        toggleSelected();
        return true;
    }
    return super.trackwheelClick(status, time);
}
private void toggleSelected() {
    _selected = !_selected;
    invalidate();
}
public void setDirty( boolean dirty )
{  
}
public void setMuddy( boolean muddy )
{ 
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:27:54+00:00Added an answer on June 13, 2026 at 3:27 am

    If you use the code below, you will get your actual requirement,you can get the slider current value into your screen class by invoking getValue() method on sliderfield reference..

    Implement FieldChangeListener interface and try to override fieldChanged() method.

    Use HorizontalFieldManager class and setStatus() method,
    if you want to display your slider bar at bottom of your screen,other wise you can display it normally.

    Here the Code:

        public class SliderScreen extends MainScreen implements FieldChangeListener
    {
    private SliderField slider;
     private HorizontalFieldManager hm;
    
    public SliderField(){
    
    slider = new SliderField(
                        Bitmap.getBitmapResource("slider2_thumb_normal.png"),
                        Bitmap.getBitmapResource("slider2_progress_normal.png"),
                        Bitmap.getBitmapResource("slider2_base_normal.png"),
                        Bitmap.getBitmapResource("slider2_thumb_focused.png"),
                        Bitmap.getBitmapResource("slider2_progress_focused.png"),
                        Bitmap.getBitmapResource("slider2_base_focused.png"), 8, 4,
                        8, 8, FOCUSABLE);
    
                slider.setChangeListener(this);
                hm = new HorizontalFieldManager();
    
    
                hm.add(slider);
    
                setStatus(hm);
    
    }
    
    
    
    public void fieldChanged(Field field, int context) {
            try {
    
                if (field == slider) {
                    int value = slider.getValue();
    
                }
    
            } catch (IllegalStateException e) {
    
                e.printStackTrace();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a project called class , and an application called students when
I have created a small daemon (basically a console application that hides the console
I have created a class that extends Application to store variables I want to
I have created a JSP / servlets application running in Tomcat 7. It runs
I have created an android application that calls (using kSOAP library) a SOAP based
I have created a Login Application with struts 2 and integrated with SSL. When
I have created a Windows Store class library. This contains some user controls and
I have created a application for Silverlight Windows Phone 7. On uploading the xap
I have created sample application that insert 12 rows in tableview.And inserted fine.When after
i have created synchronized arrayList like this import java.text.SimpleDateFormat; import java.util.*; class HelloThread {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.