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

Reply
Developer
B-Developed
Posts: 98
Registered: ‎12-29-2009
My Device: Bold
My Carrier: Rogers
Accepted Solution

Press 't' (scroll to top) and 'b' (scroll to bottom)

[ Edited ]

hi,

 

I have a verticalfieldmanager that has many fields (an unknown number as the number of fields is determined by a user-defined search).

 

I would like to implement the BB shortcut keys--specifically when the user press 't', I want the verticalfieldmanager to scroll to the top, and when the user presses 'b', I want the verticalscroll manager to scroll to the bottom.

 

(The verticalfield manager is not the only field on the screen).

 

I searched this forum, but did not find a solution.

 

Anyone know how to do this?

 

I tried the following, but it did not work:

 

VerticalFieldManager _bottmanager = new VerticalFieldManager(
			VerticalFieldManager.VERTICAL_SCROLL
					| VerticalFieldManager.VERTICAL_SCROLLBAR){
		protected boolean keyDown(int keycode, int time){
			
                        //if user presses 't', scroll to top
			if (keycode==Characters.LATIN_SMALL_LETTER_T){
				_bottmanager.setVerticalScroll(0);
			}
			return true;
			
		}
	};

 

 

Please use plain text.
Developer
B-Developed
Posts: 98
Registered: ‎12-29-2009
My Device: Bold
My Carrier: Rogers

Re: Press 't' (scroll to top) and 'b' (scroll to bottom)

[ Edited ]

Okay, the following code appears to work:

 

 

VerticalFieldManager _bottmanager = new VerticalFieldManager(
			VerticalFieldManager.VERTICAL_SCROLL
					| VerticalFieldManager.VERTICAL_SCROLLBAR){
		
public boolean keyChar(char key, int status, int time){
		boolean consumed = false;
			
   switch ( key ) {
			
	case 't':
				    
                _bottmanager.setVerticalScroll(_bottmanager.TOPMOST);
				
		break;
				
	case 'b':
				    
                setVerticalScroll(_bottmanager.BOTTOMMOST);
				
                break;
		}
			
   return super.keyChar(key, status, time);
		
 }
		
	};

 Two problems:

1.  When I press the 't' key, the fieldmanager scrolls to the top, however the field within that manager that is currently selected, is still highlighted/selected.  How do I select the top-most field in this manager?

 

2.  When I press the 'b' key, the fieldmanager scrolls to the top--why is that?

 

Anyone have any ideas?  Any help would be most appreciated.

 

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Press 't' (scroll to top) and 'b' (scroll to bottom)

Managers will, in my experience, try to ensure that the currently focused Field is displayed.  So if you want to scroll to the top, you need to find a focusable Field at the top, put focus there, and then set the VerticalScroll,

 

Something like

 

 if (keycode==Characters.LATIN_SMALL_LETTER_T){

for ( int i = 0; i < _bottmanager.getFieldCount(); i++ ) [

Field f = _bottmanager.getField(i);

if ( f.isFocusbale() {

f.setFocus();

break;

}

}
_bottmanager.setVerticalScroll(0); // Not sure this is needed
}

 

Not sure this will compile, but you get the idea I hope,

 

Obviously the reverse is required working back from the bottom for the letter B.

 

Let us know if this doesn't work for you.

Please use plain text.
Developer
B-Developed
Posts: 98
Registered: ‎12-29-2009
My Device: Bold
My Carrier: Rogers

Re: Press 't' (scroll to top) and 'b' (scroll to bottom)

[ Edited ]

Hi Peter,

 

Thank you for that helpful recommendation.

 

I don't think I provided a comprehensive enough explanation of what I was trying to achieve, or the state of my app during runtime.

 

I have a loop which pulls data from an external source.  With each piece of data that is pulled during the loop cycle, a new custom_searchresultsfield is added.  This custom_searchresultsfield has a custom "onFocus" and "unFocus" behaviour, wherein the background colour of the custom_searchresultsfield changes to blue when it has focus (when the user navigates over) and back to white when it looses focus.

 

What I tried was to set the keyChar behaviour for the customtext field such that if the user ever presses 't', the verticalscrollmanager scrolls to the top, and the backgroundcolour of the custom_searchresultsfield is changed to white.  This appears to work, but only in a very limited way.  I'm not able to get the uppermost custom_searchresultsfield to select and highlight in blue because I'm not sure how to call it.

 

Also, when the user presses 'b', the verticalfieldmanager scrolls to the top.  Not sure why that is--it does not work at all.

 

Here is the code that I am using:

 

 

public class Custom_SearchResultsField extends EditField {

public boolean keyChar(char key, int status, int time){
	boolean consumed = false;
	Background inactivebgclr;
	switch ( key ) {

	case 't':
	//set colour of selected field to white
	inactivebgclr = BackgroundFactory
	.createSolidBackground(Color.WHITE);
	setBackground(inactivebgclr);
	invalidate();								    
        _bottmanager.setVerticalScroll(_bottmanager.TOPMOST);
	//!!!CODE HERE TO SET FOCUS OF UPPERMOST CUSTOMEDITFIELD!!!
        return true;
			
        //scroll to the bottom on 'b' press
        //THIS DOES NOT WORK--SCROLLS TO TOP INSTEAD
	case 'b':
	_bottmanager.setVerticalScroll(_bottmanager.BOTTOMMOST);
	return true;
	}
	return super.keyChar(key, status, time);
}
}

Any ideas?

 

Thanks so much for your help, Peter!

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Press 't' (scroll to top) and 'b' (scroll to bottom)

I'm confused, because I thought what you have asked for was exactly what I provided.  Can you please review again the code and explanation in the previous post, and tell me what I've missed?

 

But just to confirm, when the user presses t, the processing should set the focus to the first Field in the VerticalFieldManager.  Just setting the vertical scroll will only help if the currently focused Field is visible with a VerticalScroll of 0.  If not, the scroll will, in my experience, be ignored. 

 

This processing is not dependent on how or when the Fields are added to the screen, nor any changes that actually occur when the focus is given to one of these Fields.  This is processing that applies to any Manager. 

 

I've never used and don't understand the TOPMOST and BOTTOMMOST values.  I've always scrolled to a specific position.  I would see if I can find out what these values actually are.

Please use plain text.
Developer
B-Developed
Posts: 98
Registered: ‎12-29-2009
My Device: Bold
My Carrier: Rogers

Re: Press 't' (scroll to top) and 'b' (scroll to bottom)

[ Edited ]

Hi Peter,

 

Quite right.  Sorry for the confusion, Peter.   I implemented your code and it worked successfully--I made just a few minor tweaks.

 

Here's the end result:

 

 

public boolean keyChar(char key, int status, int time){

Field f;

//scroll to the top on 't' press
case 't':
     _bottmanager.setVerticalScroll(_bottmanager.TOPMOST);
     f = _bottmanager.getField(2); //the second field from the top is always the    
                                   //uppermost field that I want selected
     f.setFocus();

     return true;

//scroll to the bottom on 'b' press

case 'b':
     _bottmanager.setVerticalScroll(_bottmanager.BOTTOMMOST);
     int i = _bottmanager.getFieldCount();
     f = _bottmanager.getField(i-1);  //getFieldCount gets total number of fields
                                      //bottommost field is total number of fields - 1
     f.setFocus();

     return true;

}//end keyChar
				

 

Thanks Peter!!!

 

 

Interesting how declaring a new field (f in the example above), and setting it to the index value within the _bottmanager makes it the same as the field that exists in the _bottmanager.  

 

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Press 't' (scroll to top) and 'b' (scroll to bottom)

[ Edited ]

"Interesting how declaring a new field (f in the example above), and setting it to the index value within the _bottmanager makes it the same as the field that exists in the _bottmanager."

 

I think you are confused between how Java handles primitives and Objects.

 

"Field f;"

This statement does not create a new Object - it merely creates a reference to an Object, in this case a Field Object.  To create an Object you must use "new".

 

"f = _bottmanager.getField(i-1);"

this just sets the reference f, to "point at" (reference) the Field identified.  It does not create a new Object either.

 

Objects in Java can have multiple different references. 

 

Anyway, I'm glad it works.

Please use plain text.
Developer
B-Developed
Posts: 98
Registered: ‎12-29-2009
My Device: Bold
My Carrier: Rogers

Re: Press 't' (scroll to top) and 'b' (scroll to bottom)

Thanks for that explanation, Peter.  You're help is always appreciated!

 

Please use plain text.
Contributor
tjerkw
Posts: 40
Registered: ‎05-12-2010
My Device: multiple devices
My Carrier: multiple simcards

Re: Press 't' (scroll to top) and 'b' (scroll to bottom)

[ Edited ]

I have a better implementation for you all... just extend TopBottomVerticalFieldManager instead of VerticalFieldManager and the (t)op and (b)ottom behaviour comes for free!

 

 

package com.hyves.bb.general.ui.component;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.container.VerticalFieldManager;

/**
 * VerticalFieldManager that has functionality for (t)op and (b)ottom jumping behaviour.
 * This only works if the keyChar was not consumed by something else. So if there is another
 * component consuming the keypress t or b then it will not work. But that is intended.
 *
 * @author Tjerk Wolterink tjerkw@gmail.com
 */
public class TopBottomVerticalFieldManager extends VerticalFieldManager {

	public TopBottomVerticalFieldManager(long style) {
		super(style);
	}

	/**
	 * Implementation of (t)op and (b)ottom jumping behaviour
	 */
	public boolean keyChar(char ch, int status, int time) {
		if(super.keyChar(ch, status, time)) {
			// event was consumed
			return true;
		}
		if(ch == 't') {
			// move to top
			Field f = this.getField(0);
			if(f!=null) {
				f.setFocus();
				this.setVerticalScroll(0);
				return true;
			}
			
		} else if(ch == 'b' && this.getFieldCount()>0) {
			// move to bottom
			Field f = this.getField(this.getFieldCount()-1);
			if(f!=null) {
				int pos = getVirtualHeight() - getVisibleHeight();
				if (pos < 0) {
					pos = getVisibleHeight();
				}
				this.setVerticalScroll(pos);
				f.setFocus();
				return true;
			}
		}
		return false;
	}

}

Please use plain text.