Domanda

I have developed a BlackBerry Application with a simple chat feature. The chat messages appear inside a Vertical Field Manager (VFM) when the user clicks Send. The user can sent multiple messages. Every new message sent appears below the older message. This way, the size of the Vertical Field Manager keeps increasing. I have created a scroll-able VFM and so the user can view the latest message sent by scrolling down. Is it possible to auto-scroll to the bottom of the VFM when a message is sent so that the focus is set on the last message?

I have tried calling vfm.setVerticalScroll(int y); as well as LabelField chat1 = new LabelField( "Hi", Field.FIELD_LEFT|Field.FOCUSABLE); but this is not helping. Please guide.

vr2 = Vertical Field Manager

chat1 = Chat message added to the VFM


EDIT: I came across this link on StackOverFlow and applied changes in my code as follows:

vr2.setVerticalScroll(vr2.getVirtualHeight());
LabelField chat1 = new LabelField( "Hi", Field.FIELD_LEFT)
{
 protected void applyTheme(Graphics g, boolean arg1) 
     {
        g.setColor(Color.DEEPSKYBLUE);
        super.applyTheme(g, arg1);
     }
};
Font myFont = Font.getDefault().derive(Font.BOLD, 8, Ui.UNITS_pt);
chat1.setFont(myFont);
chat1.setBorder( leftBorder );
chat1.setMargin( 0, 0, -15, 0);
vr2.setBorder(bdr);
vr2.add(chat1);
Field f;
int i = vr2.getFieldCount();
f = vr2.getField(i-1);  //getFieldCount, gets total number of fields
f.setFocus();

This has brought me very close to the solution I am looking for. The VFM is scrolling below fine. However, instead of positioning to the last message in the VFM, it scrolls to the second last message every time. Why is this so? It should scroll to the last message in the VFM; right at the bottom of VFM.

È stato utile?

Soluzione

I finally managed to get this solved. I will share my answer in case there are others out there stuck with such an issue. While the edit in my question above helped me come closer to the solution, I figured out the mistake was in not calling vr2.setVerticalScroll(vr2.getVirtualHeight()); at the right place.

For details, please check this question on stackoverflow and this on blackberry support forums. The solution is as follows:

LabelField chat1 = new LabelField( "Hi", Field.FIELD_LEFT)
{
 protected void applyTheme(Graphics g, boolean arg1) 
     {
        g.setColor(Color.DEEPSKYBLUE);
        super.applyTheme(g, arg1);
     }
};

Font myFont = Font.getDefault().derive(Font.BOLD, 8, Ui.UNITS_pt);
chat1.setFont(myFont);
chat1.setBorder( leftBorder );
chat1.setMargin( 0, 0, -15, 0);
vr2.setBorder(bdr);
vr2.setVerticalScroll(vr2.getVirtualHeight()); //scrolls to the bottom of the vertical field manager
vr2.add(chat1);
Field f;
int i = vr2.getFieldCount();
f = vr2.getField(i-1);  //getFieldCount, gets total number of fields
f.setFocus(); //sets focus on the newly added field
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top