Welcome!

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

Sample Code - Programmatically scroll a browser field

by Retired on ‎07-15-2010 08:35 AM - edited on ‎09-20-2010 05:00 PM by Retired (4,561 Views)

 

This article demonstrates how you can provide scrolling of browser field content in response to the following user-initiated key-press events:

 

  • SPACE - scroll one page down
  • SHIFT + SPACE - scroll one page up
  • T - scroll to the top
  • B - scroll to the bottom

 

This technique can be used for both the BrowserField class introduced in JDE 5.0, as well as the original embedded browser field solution (which uses the RenderingSession and BrowserContent classes) introduced in JDE 4.1.

 

 

The following three steps are required:

 

  1. Add the browser field object to a VerticalFieldManager
  2. Register a custom KeyListener with the screen that will contain the browser content
  3. Add the VerticalFieldManager to the screen

 

 

/**
* How the BrowserField is created:
 *
* 5.0 (See "Create your first BrowserField2 application" article)):
* BrowserField _browserField = new BrowserField();
*
* Pre 5.0 (See "BrowserFieldDemo" sample in BlackBerry JDE):
* Field _browserField = browserContent.getDisplayableContent();
*/


//Wrap the BrowserField in a VerticalFieldManager. This will then be
// used by a keyListener object to provide scrolling in response to
// key-press events.
//
VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL);
vfm.add(_browserField);

MyKeyListener _keyListener = new MyKeyListener(vfm);
_screen.addKeyListener(_keyListener);

_screen.add(vfm);
pushScreen(_screen);

 

The MyKeyListener object show in the above code snippet provides responses to key-press events.  This class implements the KeyListener class.  The below example shows how to implement this class to provide scrolling behaviour for the aforementioned key events:

 

 

 

 

 

package samplecode;

import net.rim.device.api.system.Characters;
import net.rim.device.api.system.KeyListener;
import net.rim.device.api.system.KeypadListener;
import net.rim.device.api.ui.Manager;

/**
* @description - Demonstration of using the KeyListener class to respond to user-initiated key events,
* @version 1.0
* @author Adam Stanley (astanley@rim.com), Developer Relations
* @category BlackBerry Application Development: KeyListener API
* @since June 3, 2010
* @see http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/system/KeyListener.html
*/

public class MyKeyListener implements KeyListener
{
private Manager _manager;


/**
* Constructor
* @param browserFieldManager - reference to a
* manager that contains the browserField object
*/
public MyKeyListener(Manager browserFieldManager)
{
try
{
//Save a reference to the Manager used to contain the browserField
_manager = browserFieldManager;
}
catch (Exception e) {
System.out.println("[" + this.getClass().getName() + "->constructor] Exception: " + e.getMessage() + ". " + e.toString());
e.printStackTrace();
}
}

/**
* keyChar - listener event that occurs
* whenever the user presses a BlackBerry key
*/
public boolean keyChar(char key, int status, int time)
{
int currentScrollPosition = 0;
int pageTopPosition = 0;
int pageBottomPosition = 0;
int scrollAmount = 0;

int targetScrollPosition = 0;

try
{
if (_manager == null)
{
return false;
}


/**
* Retrieve coordinates of the given field manager.
*/
currentScrollPosition = _manager.getVerticalScroll();
pageTopPosition = _manager.getTop();
pageBottomPosition = _manager.getHeight();


/**
* Define a fixed unit of height used for each scroll event.
* Can be a constant value (e.g. "100") or another quantifier.
* Choose to scroll one 'page' at a time:
 */
scrollAmount = _manager.getVisibleHeight();


/**
* Handle specific key events:
*/
switch(key)
{
case Characters.SPACE:
{
//SPACE = down; SHIFT + SPACE = up
if ((status & KeypadListener.STATUS_SHIFT) == 0)
{
//Scroll focus one unit DOWN:
_manager.getScreen().scroll(Manager.DOWNWARD);

//Scroll the screen content down one page:
targetScrollPosition = ((currentScrollPosition + scrollAmount) > pageBottomPosition) ? pageBottomPosition : (currentScrollPosition + scrollAmount);
_manager.setVerticalScroll(targetScrollPosition);
}
else
{
//Scroll focus one unit UP:
_manager.getScreen().scroll(Manager.UPWARD);

//Scroll the screen content up one page:
targetScrollPosition = ((currentScrollPosition - scrollAmount) < pageTopPosition) ? pageTopPosition : (currentScrollPosition - scrollAmount);
_manager.setVerticalScroll(targetScrollPosition);
}
return true;

}
case 't':
{
//t = TOP
//Move focus to the first element:
_manager.getScreen().scroll(Manager.TOPMOST);

//Scroll the browser content to the top
targetScrollPosition = pageTopPosition;
_manager.setVerticalScroll(targetScrollPosition);
return true;

}
case 'b':
{
//b = BOTTOM
//Move focus to the last element:
_manager.getScreen().scroll(Manager.BOTTOMMOST);

//Scroll the browser content to thr start of
// the bottom page of the current screen:
targetScrollPosition = (pageBottomPosition - scrollAmount);
_manager.setVerticalScroll(targetScrollPosition);
return true;
}
}
}
catch (Exception e) {
System.out.println("[" + this.getClass().getName() + "->keyChar] Exception: " + e.getMessage() + ". " + e.toString());
e.printStackTrace();
}

//return true only if the event was consumed.
return false;
}


/**
* The following methods must be implemented to satisfy the interface,
* however they are not used in this example:
*/
public boolean keyDown(int keycode, int time) {
return false;
}

public boolean keyRepeat(int keycode, int time) {
return false;
}

public boolean keyStatus(int keycode, int time) {
return false;
}

public boolean keyUp(int keycode, int time) {
return false;
}

}

 

 

 

 

See the following learning resources for more information on the APIs used in this article:

 

Users Online
Currently online: 45 members 6,896 guests
Please welcome our newest community members: