Date Created: Sun 26-Jun-2011

Related Document Categories:

Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/


    BlackBerry Java Code to create a menu Item

    Below is the main function to created a menu item in a BB screen., This function opens the Configuration screen

    protected void makeMenu(Menu menu, int instance) {
    MenuItem aboutMe = new MenuItem("About Me", 10, 10) {

    public void run() {
    try {
    UiApplication.getUiApplication().pushScreen(new AboutMeScreen());
    }
    catch (Exception e)
    {
    System.out.println(e.getMessage());
    System.out.println(e.getClass().toString());
    }

    }
    };
    menu.add(aboutMe);
    MenuItem config = new MenuItem("Configuration", 10, 10) {

    public void run() {
    UiApplication.getUiApplication().pushScreen(new ConfigScreen());
    }
    };
    menu.add(config);
    }


    ====================


    package com.screv.screens;

    import net.rim.device.api.system.Bitmap;
    import net.rim.device.api.ui.Field;
    import net.rim.device.api.ui.FieldChangeListener;
    import net.rim.device.api.ui.Font;
    import net.rim.device.api.ui.UiApplication;
    import net.rim.device.api.ui.component.EditField;
    import net.rim.device.api.ui.component.LabelField;
    import net.rim.device.api.ui.container.HorizontalFieldManager;
    import net.rim.device.api.ui.container.MainScreen;
    import net.rim.device.api.ui.container.VerticalFieldManager;
    import net.rim.device.api.ui.decor.Background;
    import net.rim.device.api.ui.decor.BackgroundFactory;

    import com.screv.main.StaticVals;
    import com.scren.util.Utils;
    import com.screv.fields.BitmapButtonField;

    public class ConfigScreen extends MainScreen implements FieldChangeListener {
    BitmapButtonField saveButton;
    EditField url;

    public ConfigScreen() {
    //Background mainBG = BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("images/login_background.png"));
    Background mainBG = BackgroundFactory.createBitmapBackground(Utils.getBitmap("images/login_background.png"));
    VerticalFieldManager mainVerticalManager = new VerticalFieldManager(
    USE_ALL_HEIGHT | USE_ALL_WIDTH
    | VerticalFieldManager.VERTICAL_SCROLL
    | VerticalFieldManager.VERTICAL_SCROLLBAR);
    mainVerticalManager.setBackground(mainBG);
    mainVerticalManager.setPadding(5, 5, 5, 25);
    Font font = Font.getDefault();
    font = font.derive(Font.PLAIN, 15);
    /*Background editText = BackgroundFactory.createBitmapBackground(Bitmap
    .getBitmapResource("editText.png"), Background.POSITION_X_LEFT,
    Background.POSITION_Y_TOP, Background.REPEAT_SCALE_TO_FIT);*/
    Background editText = BackgroundFactory.createBitmapBackground(Utils.getBitmap("images/editText.png"), Background.POSITION_X_LEFT,
    Background.POSITION_Y_TOP, Background.REPEAT_SCALE_TO_FIT);

    LabelField lblUrl = new LabelField("Web-Service URL :");
    lblUrl.setBackground(editText);
    lblUrl.setFont(font);
    lblUrl.setPadding(5, 5, 5, 5);

    mainVerticalManager.add(lblUrl);
    url = new EditField("", StaticVals.urlLogin);
    url.setBackground(editText);
    url.setPadding(5, 5, 5, 5);
    font = Font.getDefault();
    font = font.derive(Font.PLAIN, 15);
    url.setFont(font);
    mainVerticalManager.add(url);

    HorizontalFieldManager buttonManager = new HorizontalFieldManager(
    HorizontalFieldManager.FIELD_HCENTER);
    saveButton = new BitmapButtonField("save_button_pressed.png",
    "save_button.png");
    saveButton.setChangeListener(this);
    saveButton.setPadding(5, 5, 5, 5);
    buttonManager.add(saveButton);
    BitmapButtonField cancelButton = new BitmapButtonField(
    "cancel_button_pressed.png", "cancel_button.png");
    cancelButton.setPadding(5, 5, 5, 5);
    cancelButton.setChangeListener(this);
    buttonManager.add(cancelButton);
    mainVerticalManager.add(buttonManager);
    add(mainVerticalManager);

    }

    public void fieldChanged(Field field, int context) {
    if (field == saveButton) {
    StaticVals.urlLogin = url.getText();

    }
    UiApplication.getUiApplication().popScreen(this);
    }

    =======================

    package com.screv.fields;

    import net.rim.device.api.system.Bitmap;
    import net.rim.device.api.ui.Field;
    import net.rim.device.api.ui.Graphics;
    import net.rim.device.api.ui.Keypad;

    /**
    * Button field with a bitmap as its label.
    */

    public class BitmapButtonField extends Field {

    private Bitmap bitmap;
    private Bitmap unFocusBitmap;

    /**
    * Instantiates a new bitmap button field.
    *
    * @param bitmap
    * the bitmap to use as a label
    */

    public BitmapButtonField(String focusimagefile, String unfocusImagePath) {
    super(Field.FOCUSABLE);

    bitmap = Bitmap.getBitmapResource(focusimagefile);
    unFocusBitmap = Bitmap.getBitmapResource(unfocusImagePath);

    }

    public BitmapButtonField(String focusimagefile, String unfocusImagePath,
    long style) {
    super(style);
    bitmap = Bitmap.getBitmapResource(focusimagefile);
    unFocusBitmap = Bitmap.getBitmapResource(unfocusImagePath);

    }

    protected void layout(int width, int height) {
    setExtent(bitmap.getWidth(), bitmap.getHeight());
    }

    protected void paint(Graphics graphics) {

    if (isFocus()) {
    graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),
    bitmap, 0, 0);
    } else {
    graphics.drawBitmap(0, 0, unFocusBitmap.getWidth(), unFocusBitmap
    .getHeight(), unFocusBitmap, 0, 0);
    }
    }

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

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

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

    protected boolean keyChar(char character, int status, int time) {
    if (character == Keypad.KEY_ENTER) {
    fieldChangeNotify(0);
    return true;
    }
    return super.keyChar(character, status, time);
    }

    protected boolean navigationClick(int status, int time) {
    fieldChangeNotify(0);
    return true;
    }

    }
    }

    Get my WebSphere Application Server course here >> http://www.themiddlewareshop.com/products/


Steve Robinson - IBM Champion 2013

About Me

Steve Robinson has been working in IT for over 20 years and has provided solutions for many large-enterprise corporate companies across the world. Steve specialises in Java and Middleware.

In January 2013, I was awarded the prestigous 'IBM Champion' accolade.


Read my books?

IBM WebSphere Application Server 8.0 Administration Guide

IBM WebSphere Application Server 8.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Application Server 7.0 Administration Guide

WebSphere Categories

Other Categories