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
Posts: 47
Registered: ‎03-17-2009
My Device: Not Specified

VerticalFieldManager hides part of field when scrolling

Hello,

 

I have a main VFM and a CustomFieldManager, where the CustomFieldManager (CFM) extends a VFM and has a height and width of 50.  Creating 5 CFM's and adding them to the main VFM allows me to scroll and select each CFM but when i get past the height of the screen, (so at the 5th CFM), only part of it is displayed.  Meaning that only a few pixels are displayed instead of the entire 50.  I have to keep scrolling for the entire 50 to be displayed.  However i know this field is in focus as i have the focused fields colored.  So basically i'm trying to get the entire field to show as i scroll down.  I guess I didn't explain this too well so here's some pictures and code.  This is my first time posting so sorry if the format pasting the code gets messed!

 

The 5th field is highlighted but not fully displayed:

 

 

 

But when i scroll down some more, the entire field is displayed:

 

 

 

 

Here's the code for the CustomFieldManager:

 

 

public class ThumbnailFieldManager extends VerticalFieldManager implements IntegerConstants {

private int numRows;
private int style;
private int managerHeight;
private int columns = 1;

public ThumbnailFieldManager(int _numRows, long _style) {
//super(Manager.VERTICAL_SCROLL);
//style = _style;
super();
numRows = _numRows;
managerHeight = (getPreferredHeight() * numRows);
}

protected void sublayout(int width, int height) {

Field field;
int x = 0;
int y = 0;

for (int i = 0; i < getFieldCount(); i++) {

field = getField(i);

layoutChild(field, width, getPreferredHeight());

long fieldStyle = field.getStyle();
int fieldXOffset = 0;
long fieldHalign = fieldStyle & Field.FIELD_HALIGN_MASK;

if (fieldHalign == Field.FIELD_RIGHT) {
fieldXOffset = THUMBNAIL_WIDTH - field.getWidth();
}
else if (fieldHalign == Field.FIELD_HCENTER) {
fieldXOffset = ((THUMBNAIL_WIDTH-field.getWidth())/2); }

int fieldYOffset = 0;
long fieldValign = fieldStyle & Field.FIELD_VALIGN_MASK;

if (fieldValign == field.FIELD_BOTTOM) {
fieldYOffset = HEADLINE_HEIGHT - field.getHeight();
}
else if (fieldValign == Field.FIELD_VCENTER) {
fieldYOffset = ((HEADLINE_HEIGHT/2) - ((field.getHeight())/2));
}

setPositionChild(field, x+fieldXOffset, y+fieldYOffset);

y += getPreferredHeight();

}

setExtent(THUMBNAIL_WIDTH, managerHeight);
}


public int getPreferredWidth() {
return THUMBNAIL_WIDTH;
}

public int getPreferredHeight() {
return HEADLINE_HEIGHT;
}

protected void onUnfocus() {
super.onUnfocus();
invalidate();
}

public boolean isFocusable() {
return true;
}

protected void drawFocus(Graphics graphics, boolean on) {
// Don't draw the default focus
}


protected void onFocus(int direction) {
super.onFocus(direction);
invalidate();
}

public void paintBackground( Graphics g ) {
g.clear();
int color = g.getColor();
g.setColor( Color.BLACK );
g.fillRect( 0, 0, THUMBNAIL_WIDTH, managerHeight);
g.setColor( color );
}

public void paint(Graphics g)
{

if (this.isFocus()) {
g.setBackgroundColor(Color.BLUE);
g.clear();
} else {
g.setBackgroundColor(Color.ORANGE);
g.clear();
}
super.paint(g);
}
}

 

 

And here's the code for the screen that implements a VFM and adds the CFM:

  

class HeadlinesScreen extends MainScreen implements IntegerConstants {

private ThumbnailFieldManager thumbnails;
private VerticalFieldManager vfm;

HeadlinesScreen() {
super();
VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Field.FOCUSABLE);
vfm.add(new LabelField("testXX"));

LabelField lf[] = {new LabelField("1", Field.FIELD_VCENTER | Field.FIELD_HCENTER), new LabelField("2"), new LabelField("3", Field.FOCUSABLE | Field.FIELD_VCENTER | Field.FIELD_HCENTER), new LabelField("4"), new LabelField("5testtttt")};

for(int i = 0; i < 5; i++) {
thumbnails = new ThumbnailFieldManager(1, 0);
thumbnails.add(lf[i]);
vfm.add(thumbnails);
vfm.add(new SeparatorField());
}

vfm.add(new LabelField("testYY"));
add(vfm);

}

}

 

 

 

What am i doing wrong??

 

Thanks in advance,

Andrew

Developer
Posts: 178
Registered: ‎07-29-2008
My Device: Not Specified

Re: VerticalFieldManager hides part of field when scrolling

ok, took me three tries to get this make sence, you need to override "getFocusRect()" for the manager (or for the label)  and return the size of the region you want to have focus drawn for (ie the size of your box)  this also dictates scroll size.
Developer
Posts: 696
Registered: ‎12-02-2008
My Device: Not Specified

Re: VerticalFieldManager hides part of field when scrolling

Hi,

 

I think you should set both your label's ("testXX") and ("testYY") using setTitle() and setStatus() of MainScreen and

subtract heigth of both these labels from your manager.

 

I think this should help you.

 

Regards,
Rajat Gupta.
--------------------------------------------------------------------------------
If your problem was get solved then please mark the thread as "Accepted solution" and kudos - your wish
Developer
Posts: 178
Registered: ‎07-29-2008
My Device: Not Specified

Re: VerticalFieldManager hides part of field when scrolling

Thanks not going to help with the actual problem, only with this specific case.  the issue is his focus rect that is being returned does not match the focus rect that he desires to be shown. 
Developer
Posts: 47
Registered: ‎03-17-2009
My Device: Not Specified

Re: VerticalFieldManager hides part of field when scrolling

Thanks for the replies.

 

I've tried adding the method

 

public void getFocusRect(XYRect rect) { rect.x = 0; rect.y = 0; rect.height = HEADLINE_HEIGHT; rect.width = THUMBNAIL_WIDTH; }

 

 

but it still doesn't move focus onto the 5th field.  Am I supposed to change any of the above code?  I did not touch anything else.

Developer
Posts: 178
Registered: ‎07-29-2008
My Device: Not Specified

Re: VerticalFieldManager hides part of field when scrolling

[ Edited ]

wait, is focus not moving or is the manager not scrolling?  (had thought the issue was the latter, if so follow my instructions)

 

The simplest way is to use the method you added, but subclass LabelField with is.

 

 

LabelField lf[] = {new MyLabelField("1", Field.FIELD_VCENTER | Field.FIELD_HCENTER), new MyLabelField("2"), new MyLabelField("3", Field.FOCUSABLE | Field.FIELD_VCENTER | Field.FIELD_HCENTER), new MyLabelField("4"), new MyLabelField("5testtttt")};

 

 and then

 

 

class MyLabelField extends LabelField{
public void getFocusRect(XYRect rect) {
rect.x = 0;
rect.y = 0;
rect.height = HEADLINE_HEIGHT;
rect.width = THUMBNAIL_WIDTH;
}
}

There may be some scope issus with the that, but it should work pretty to close to that.

 

 

Message Edited by haagmm on 03-19-2009 12:03 PM
Developer
Posts: 47
Registered: ‎03-17-2009
My Device: Not Specified

Re: VerticalFieldManager hides part of field when scrolling

Hey Haagmn,

 

It's not the labelfield that i care about for focus - more the manager.  Or should i be caring bout the labelfield focus instead of the manager?  E.g. i want the manager to change background colour of a field when focused, without caring about the labelfield.

 

The manager is scrolling, and focus does change but just the last row doesnt entirely show when that row is on focus.

Developer
Posts: 178
Registered: ‎07-29-2008
My Device: Not Specified

Re: VerticalFieldManager hides part of field when scrolling

well, the thing is, in standard ui the manager it self can not be an END focus field.  that has to like with a non manager field, you can fake this, but its alot more work.

 

The label field is what is being shown.  the issue is its only scrolling the height of the label,  because that is the field in focus. 

 

The solution i gave you matches what i do my self in a similar situation.  


I would live a RIM responce here, as if you ALSO want to have a different scroll size vs focus size you need to actually oveerload undocumented methods.  

 

Developer
Posts: 47
Registered: ‎03-17-2009
My Device: Not Specified

Re: VerticalFieldManager hides part of field when scrolling

Hmm ok i tried it, worked like a charm.  When calling the customlabelfield i had to make the 1st and last row focusable.  Here's the code, thanks for you help!  Im curious though, is there a better/more efficient way of doing this?

 

CustomLabelField class:

 

public class CustomLabelField extends LabelField implements IntegerConstants { public CustomLabelField(Object text) { super(text); } public CustomLabelField(Object text, long style) { super(text, style); } public void getFocusRect(XYRect rect) { rect.x = 0; rect.y = 0; rect.height = HEADLINE_HEIGHT; rect.width = THUMBNAIL_WIDTH; } public void paint(Graphics g) { if (this.isFocus()) { g.setBackgroundColor(Color.GREEN); g.clear(); } else { g.setBackgroundColor(Color.PINK); g.clear(); } super.paint(g); } }

 

ThumbnailFieldManager:

 

public class ThumbnailFieldManager extends VerticalFieldManager implements IntegerConstants { private int numRows; private int style; private int managerHeight; private int columns = 1; private int selectedRowIndex = -1; private int selectedColumnIndex = -1; /*public ThumbnailFieldManager() { super(Manager.VERTICAL_SCROLL); managerHeight = getPreferredHeight(); }*/ public ThumbnailFieldManager(int _numRows, long _style) { //super(Manager.VERTICAL_SCROLL); //style = _style; super(); numRows = _numRows; managerHeight = (getPreferredHeight() * numRows); } protected void sublayout(int width, int height) { //setExtent(THUMBNAIL_WIDTH, HEADLINE_HEIGHT); //CustomTextField field; Field field; int x = 0; int y = 0; for (int i = 0; i < getFieldCount(); i++) { //field = (CustomTextField) getField(i); field = getField(i); layoutChild(field, width, getPreferredHeight()); long fieldStyle = field.getStyle(); int fieldXOffset = 0; long fieldHalign = fieldStyle & Field.FIELD_HALIGN_MASK; if (fieldHalign == Field.FIELD_RIGHT) { fieldXOffset = THUMBNAIL_WIDTH - field.getWidth(); } else if (fieldHalign == Field.FIELD_HCENTER) { fieldXOffset = ((THUMBNAIL_WIDTH-field.getWidth())/2);//((THUMBNAIL_WIDTH/2) - ((field.getWidth())/2)); } int fieldYOffset = 0; long fieldValign = fieldStyle & Field.FIELD_VALIGN_MASK; if (fieldValign == field.FIELD_BOTTOM) { fieldYOffset = HEADLINE_HEIGHT - field.getHeight(); } else if (fieldValign == Field.FIELD_VCENTER) { fieldYOffset = ((HEADLINE_HEIGHT/2) - ((field.getHeight())/2)); } setPositionChild(field, x+fieldXOffset, y+fieldYOffset); /*switch(style) { case 1: setPositionChild(field, 0, 0); break; default: setPositionChild(field, 0, 0); break; }*/ y += getPreferredHeight(); } setExtent(THUMBNAIL_WIDTH, managerHeight); } public int getPreferredWidth() { return THUMBNAIL_WIDTH; } public int getPreferredHeight() { return HEADLINE_HEIGHT; } protected void onUnfocus() { super.onUnfocus(); invalidate(); } public boolean isFocusable() { return true; } protected void onFocus(int direction) { super.onFocus(direction); invalidate(); } public void paint(Graphics g) { if (this.isFocus()) { g.setBackgroundColor(Color.BLUE); g.clear(); } else { g.setBackgroundColor(Color.ORANGE); g.clear(); } super.paint(g); } }

 

HeadlineScreen:

 

 

class HeadlinesScreen extends MainScreen implements IntegerConstants { private ThumbnailFieldManager thumbnails; private VerticalFieldManager vfm; HeadlinesScreen() { super(); VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Field.FOCUSABLE); vfm.add(new LabelField("testXX")); LabelField lf[] = {new CustomLabelField("1", Field.FOCUSABLE | Field.FIELD_HCENTER), new CustomLabelField("2"), new CustomLabelField("3", Field.FOCUSABLE | Field.FIELD_VCENTER | Field.FIELD_HCENTER), new CustomLabelField("4"), new CustomLabelField("5testtttt", Field.FOCUSABLE)}; for(int i = 0; i < 5; i++) { thumbnails = new ThumbnailFieldManager(1, 0); thumbnails.add(lf[i]); vfm.add(thumbnails); vfm.add(new SeparatorField()); } vfm.add(new LabelField("testYY")); add(vfm); }

 

 

Thanks Again!

Developer
Posts: 47
Registered: ‎03-17-2009
My Device: Not Specified

Re: VerticalFieldManager hides part of field when scrolling

So now i have another problem.  Given I have the two fields - thumbnail field, and then a headline field:

 

 

When i scroll down it selects thumbnail, then headline and so on for each row.  When selected the background of each field changes to a different color, e.g. blue.  But what i want instead is as i scroll down, the ENTIRE row - which includes the thumbnail field AND headline field -  to change color - not one field at a time, but one ROW at a time. 

 

Right now the code for the thumbnail field manager is:

 

public class ThumbnailFieldManager extends VerticalFieldManager implements IntegerConstants { private int numRows; private int managerHeight; private int column; public ThumbnailFieldManager(int _column) { super(); column = _column; managerHeight = getPreferredHeight(); } public ThumbnailFieldManager(int _numRows, int _column) { super(); numRows = _numRows; column = _column; managerHeight = (getPreferredHeight() * numRows); } protected void sublayout(int width, int height) { Field field; int x = 0; int y = 0; int field_width; field_width = (column == 1) ? THUMBNAIL_WIDTH : HEADLINE_WIDTH; for (int i = 0; i < getFieldCount(); i++) { field = getField(i); layoutChild(field, width, getPreferredHeight()); long fieldStyle = field.getStyle(); int fieldXOffset = 0; long fieldHalign = fieldStyle & Field.FIELD_HALIGN_MASK; if (fieldHalign == Field.FIELD_RIGHT) { fieldXOffset = field_width - field.getWidth(); } else if (fieldHalign == Field.FIELD_HCENTER) { fieldXOffset = ((field_width-field.getWidth())/2);//((THUMBNAIL_WIDTH/2) - ((field.getWidth())/2)); } int fieldYOffset = 0; long fieldValign = fieldStyle & Field.FIELD_VALIGN_MASK; if (fieldValign == Field.FIELD_BOTTOM) { fieldYOffset = HEADLINE_HEIGHT - field.getHeight(); } else if (fieldValign == Field.FIELD_VCENTER) { fieldYOffset = ((HEADLINE_HEIGHT/2) - ((field.getHeight())/2)); } setPositionChild(field, x+fieldXOffset, y+fieldYOffset); y += getPreferredHeight(); } setExtent(field_width, managerHeight); } public int getPreferredWidth() { if (column == 1) return THUMBNAIL_WIDTH; else return HEADLINE_WIDTH; } public int getPreferredHeight() { return HEADLINE_HEIGHT; } protected void onUnfocus() { super.onUnfocus(); invalidate(); } public boolean isFocusable() { return true; } protected void onFocus(int direction) { super.onFocus(direction); invalidate(); } public void paint(Graphics g) { if (this.isFocus()) { g.setBackgroundColor(Color.BLUE); g.clear(); } else { g.setBackgroundColor((column == 1) ? Color.ORANGE : Color.RED); g.clear(); } super.paint(g); } }

 

I'm simply creating two instances of the thumbnailFieldManager, one for thumbnail, the other for headline.  And then i'm adding each of these instances to a non-scrollable horizontal field manager.  And then i add the hfm to a main vertical field manager (scrollable). I tried changing the color and drawFocus color of the vfm but i'm definitely doing something wrong. 

 

Any suggestions?