11-11-2009 10:07 PM - edited 11-11-2009 10:17 PM
hi all,
i create simple BB app with eclipse jde 4.6.1, here the sample code,
class myApplication extends UiApplication
{
// applicatione entry point
public static void main(String[] args)
{
// create an instance of our app
myApplication theApp = new myApplication();
// "run" the app
theApp.enterEventDispatcher();
}
// app constructor
public myApplication()
{
myScreen screen = new myScreen();
pushScreen(screen);
}
}
where my screen is like this
public class myScreen extends MainScreen implements ListFieldCallback {
private Vector menu;
private ListField menuList;
private MenuItem menuItem = new MenuItem("Details",100,10){
public void run(){
int index = menuList.getSelectedIndex();
if (index == 1){
Dialog.alert("list "+index+" selected");
}
}
};
public void drawListRow(ListField listField, Graphics graphics, int index,
int y, int width) {
ListField menulist = (ListField) listField;
MenuRowManager rowManager = (MenuRowManager)menu.elementAt(index);
rowManager.drawRow(graphics, 0, y, width, menulist.getRowHeight());
}
public myScreen(){
super(DEFAULT_MENU|DEFAULT_CLOSE);
setTitle(new LabelField("myScreen", LabelField.USE_ALL_WIDTH | DrawStyle.HCENTER));
this.addMenuItem(menuItem);
createListMenu();
}
public void createListMenu(){
menu = new Vector();
menuList = new ListField(){
protected boolean keyChar(char key, int status, int time){
if (key == Characters.ENTER){
// open next window
int index = getSelectedIndex();
switch (index){
case 0: //
break;
case 1: //
break;
case 2: //
break;
case 3: //
break;
default: // about
break;
}
}
return true;
}
};
menuList.setCallback(this);
menuList.setRowHeight(60);
menuList.setBackground(BackgroundFactory.createSol idBackground(Color.BLACK));
// first item
BitmapField mIcon = new BitmapField(Bitmap.getBitmapResource("image1.png") );
LabelField lblM = new LabelField ("First list item", DrawStyle.LEFT);
lblM.setFont(Font.getDefault().derive(Font.BOLD));
MenuRowManager menuM = new MenuRowManager();
menuM.add(mIcon);
menuM.add(lblM);
menu.addElement(menuM);
// 2nd item
BitmapField tIcon = new BitmapField(Bitmap.getBitmapResource("image2.png") );
LabelField lblT = new LabelField("Theater Near Me", DrawStyle.LEFT);
lblT.setFont(Font.getDefault().derive(Font.BOLD));
MenuRowManager menuT = new MenuRowManager();
menuT.add(tIcon);
menuT.add(lblT);
menu.addElement(menuT);
add(menuList);
}
}
and the definition of menuRowManager is like this
public class MenuRowManager extends Manager
{
public MenuRowManager()
{
super(0);
}
// Causes the fields within this row manager to be layed out then
// painted.
public void drawRow(Graphics g, int x, int y, int width, int height)
{
// Arrange the cell fields within this row manager.
layout(width, height);
// Place this row manager within its enclosing list.
setPosition(x, y);
// Apply a translating/clipping transformation to the graphics
// context so that this row paints in the right area.
g.pushRegion(getExtent());
// Paint this manager's controlled fields.
subpaint(g);
g.setColor(0x00CACACA);
g.drawLine(0, 0, getPreferredWidth(), 0);
//g.drawLine(10, 0, 10, getPreferredHeight());
// Restore the graphics context.
g.popContext();
}
// Arranges this manager's controlled fields from left to right within
// the enclosing table's columns.
protected void sublayout(int width, int height)
{
// set the size and position of each field.
int fontHeight = Font.getDefault().getHeight();
int preferredWidth = getPreferredWidth();
// start with the Bitmap Field of menu icon
Field field = getField(0);
layoutChild(field, 48, 48);
setPositionChild(field, 0, 6);
// set the menu title label field
field = getField(1);
layoutChild(field, preferredWidth - 16, fontHeight+1);
setPositionChild(field, 55, 30-fontHeight/2);
setExtent(preferredWidth, getPreferredHeight());
}
// The preferred width of a row is defined by the list renderer.
public int getPreferredWidth()
{
return Graphics.getScreenWidth();
}
// The preferred height of a row is the "row height" as defined in the
// enclosing list.
public int getPreferredHeight()
{
return 60;
}
}
the problem is, when first screen showed (pushed), i cant' navigate back (close) the screen with escape button, is my code wrong? did i miss something here?
thanks you in advance
Solved! Go to Solution.
11-11-2009 10:09 PM
btw, how to write code formatting in this forum ?
sorry
11-11-2009 10:13 PM
See the littl "C" in a box, on the toolbar?
If you hover your mouse over this, you will get the tooltip that says "Insert Code"
Regarding your code question, if you push screen "A", the push screen "B", the pop screen "B", screen "A" will now be displayed. The screens are stored in a stack structure. The topmost screen is visible.
11-11-2009 10:29 PM
thanks RexDoug,
I only use one screen (pushed in UiApplication class), the problem is, after the screen showed, when i press escape button, the screen doesn't closed. but if i didn't add my custom listfield into myScreen class,
its work and the screen can be closed by pressing escape button.
public class myScreen extends MainScreen implements ListFieldCallback {
private Vector menu;
private ListField menuList;
public myScreen(){
createListMenu();
}
public void createListMenu(){
// another code goes here
// I remove this add custom list field to the screen code
// add(menuList); <<-------------- i comment this code
// and its work (click escape button will close the screen)
}
}
I'm sorry with my english
11-12-2009 12:03 AM
Did you override keyChar() in your custom field?
Don't forget that you must re-delegate any keystrokes that you do not consume.
11-12-2009 12:26 AM
ah, alhamdulillah,
yes you're right, i am missing that point, i override the keyChar but forget to handle escape key
now, its work. well done.
thank you very much RexDoug ![]()