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
Highlighted
New Contributor
Posts: 8
Registered: ‎09-01-2010
My Device: Not Specified
Accepted Solution

setFieldWithFocus on VerticalFieldManager

[ Edited ]

Hi,

 

I'm developing an application using JDE 4.2.1.

 

I'm creating a Form, containing a header and a selectable list made with a VerticalFieldManager.

This is how I create the VerticalFieldManager containing the lines

 

 

this._vfmLista = new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLLBAR)
{
protected void sublayout(int ancho, int alto)
{
ancho = ANCHO_PANTALLA;
alto = ALTO_PANTALLA - _heightOffset - ConstantesIU.SEPARATOR_HEIGHT - ConstantesIU.BOTTOM_MARGIN;
super.sublayout(ancho, alto);
super.setExtent(ancho, alto);
}
};
this.inicializarLista();
this._vfmLista.setFocusListener(this);
this._libre.anyadir(this._vfmLista, ConstantesIU.LEFT_MARGIN, _heightOffset);

 

_libre is a custom Layout which adds the VerticalFieldManager with the method 'anyadir'

The Panel I add to the VerticalFieldManager is a custom Panel (MyCustomPanel) that contains an instance of the listItem (MyCustomObject)
The Panel draws 2 RichTextFields divided in 2 columns. The first value is fixed, and the second is editable using a semi-transparent modal dialog(DialogFieldManager) which askes for the value and then exits with this.close(), so we don't dispose the calling form that is being drawed behind.

The method "inicializarLista" intializes the contents of the VerticalFieldManager disposing the previous ones and adding the new updated contents.
The selection index is managed with the global var "_numSeleccionado".
This var is being updated each time the user selects an item from the VerticalFieldManager and press 'enter' or 'trackwheelclick'. A modal Dialog is shown asking for the value and then closes itself.

 

 

 

private void inicializarLista()
{
MyCustomObject listItem;
MyCustomPanel loopPanel;
try
{
this._vfmLista.deleteAll(); //We delete the previous contents of the VerticalFieldManager
if (this._listItemVector == null) //This only runs first time when the listItemVector is empty
{
this._listItemVector = this._actividad.getListData();
}
if (this._listItemVector != null && this._listItemVector.size() > 0)
{
for (int i = 0; i < this._listItemVector.size(); i++)//Iterate the listItemVector
{
listItem = (MyCustomObject) this._listItemVector.elementAt(i);
loopPanel = new MyCustomPanel(listItem);//For each listItem we create a new Panel
this._vfmLista.add(loopPanel);//We add the Panel to the VerticalFieldManager
}
int numSeleccionado = 0;
if(this._numSeleccionado != null)
{
numSeleccionado = Integer.parseInt(this._numSeleccionado) - 1;//We obtain the previous selection minus one (index from 0 to 9)
}
this._vfmLista.setFieldWithFocus(this._vfmLista.getField( numSeleccionado ));//We give the focus to the field located at "numSeleccionado" position of the VerticalFieldManager
}
}
catch (Exception ex)
{
Log.getInstancia().escribir("FormList.inicializarLista", "Excepción: " + ex.getMessage());
}
}

 

MyCustomPanel implements the following methods for the focus handling:

private void notifyFocus ( int evento )
{
try
{
FocusChangeListener focusListener;
focusListener = this.getFocusListener ();
if ( focusListener != null )
{
focusListener.focusChanged ( this, evento );
focusListener = null;
}
}
catch ( Exception ex )
{
Log.getInstancia().escribir("MyCustomPanel.notifyFocus", "Excepción: " + ex.getMessage());
}
}

protected void drawFocus ( Graphics graphics, boolean activado )
{
}

protected void onFocus ( int direccion )
{
try
{
this.setBorde ( this._borde );
this.notifyFocus ( FocusChangeListener.FOCUS_GAINED );
}
catch ( Exception ex )
{
Log.getInstancia().escribir("MyCustomPanel.onFocus", "Excepción: " + ex.getMessage());
}
}

protected void onUnfocus ()
{
try
{
this.setBorde ( null );
this.notifyFocus ( FocusChangeListener.FOCUS_LOST );
}
catch ( Exception ex )
{
Log.getInstancia().escribir("MyCustomPanel.onUnFocus", "Excepción: " + ex.getMessage());
}
}

protected int moveFocus ( int cantidad, int estado, int tiempo )
{
return cantidad;
}

public boolean isFocusable ()
{
return true;
}

public Field getLeafFieldWithFocus ()
{
return this;
}

public MyCustomObject getListItem()
{
return this._listItem;
}

 

 

The problem I have is when I return from the modal Dialog and call again the "inicializarLista" method.
When I rebuild the VerticalFieldManager with the new contents, it selects automatically the first item in the manager.

Although I use the method setFieldWithFocus for selecting the previously selected item, it keeps selecting the first item.

¿Any clue or anyone with the same problem?.

Note: at this moment i'll try to override the setFieldWithFocus method on the VerticalFieldManager's creation and iterate all the elements until I find the "previously selected one", and then call 'setFocus' on it. I'll keep updated if it works.

 

Thanks in advance!

New Contributor
Posts: 8
Registered: ‎09-01-2010
My Device: Not Specified

Re: setFieldWithFocus on VerticalFieldManager

Solved myself.

 

It was easier than I initially thought ( I just had a feeling of deja vu Smiley Wink)

 

I made the following changes in the creation of the VerticalFieldManager

 

 

private void inicializarLista()
{
MyCustomObject listItem;
MyCustomPanel loopPanel;
try
{
this._vfmLista.deleteAll(); //We delete the previous contents of the VerticalFieldManager
if (this._listItemVector == null) //This only runs first time when the listItemVector is empty
{
this._listItemVector = this._actividad.getListData();
}
if (this._listItemVector != null && this._listItemVector.size() > 0)
{
for (int i = 0; i < this._listItemVector.size(); i++)//Iterate the listItemVector
{
listItem = (MyCustomObject) this._listItemVector.elementAt(i);
loopPanel = new MyCustomPanel(listItem);//For each listItem we create a new Panel
this._vfmLista.add(loopPanel);//We add the Panel to the VerticalFieldManager
if(this._numSeleccionado != null)
{
if(listItem.getNum().equals(this._numSeleccionado))
{
loopPanel.setFocus();
}
}
}
}
}
catch (Exception ex)
{
Log.getInstancia().escribir("FormList.inicializarLista", "Excepción: " + ex.getMessage());
}
}

 Instead of setting the focus after the creation of all elements, during creation of the Panel I check if the current item should receive the focus and then call setFocus on it after the add call.