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
Regular Contributor
zumwaltwood
Posts: 81
Registered: ‎01-14-2011
My Device: Curve 8530
Accepted Solution

?? why "MenuItem(String, int, int)" deprecated in 1.3?

This has been bugging me but didn't really think about posting until now, in the 1.3 release of the SDK, MenuItem(String, int, int) is marked as deprecated, but there is no other constructor for it, so why is it marked deprecated without another constructor? So basically in my "Problems" section, I have "Problems" on ALL MenuItems as warnings about it being deprecated, but I have absolutely NO fix for it because there is no other visible constructor?

 

Furthermore, in the "javadoc" for MenuItem, this is what it shows

 

[QUOTE]

net.rim.device.api.ui.MenuItem

Represents an abstract menu item.

Each item has an ordinal specifying the sort order within the menu. A separator is automatically inserted between adjacent items whose ordinals differ by at least 0x00010000. Each item also has a priority specifying which one should be the default. A lower value indicates a higher priority.

Creating menu items by using commands

Since BlackBerry API 6.0, you can use the Command Framework API to create a menu item. The process is as follows:

  1. Define the functionality to be performed by using a CommandHandler. The command handler might be limited to your application, or might be registered so it is available to any application on the BlackBerry device.
  2. In your screen, optionally specify the context of the menu item by invoking setCommandContext(). The context might be required in order to determine what the command handler should do.
  3. Associate the menu item with a Command by invoking setCommand(). The Command defines what to execute when the menu item is selected by acting as a proxy to an instance of a class that extends a command handler. The command is only executed if there is no implementation of Runnable. If the context was specified, it is passed to the command's execute method. Otherwise, this menu item is passed.

Code sample

 class MyUiScreen extends MainScreen
 {
     public MyUiScreen()
     {
       //...
       // Create MenuItem with command context
       MenuItem myItem = new MenuItem("My Menu Item", 110, 10);
       myItem.setCommandContext(new Object()
       {
           public String toString()
           {
               return "My MenuItem Object"; 
           }          
       });
       // Set command to be invoked by the MenuItem
       myItem.setCommand(new Command(new DialogCommandHandler()));
      
       addMenuItem(myItem);
     }

 // A CommandHandler implementation which will be executed unconditionally
 class DialogCommandHandler extends CommandHandler
 {
     public void execute(ReadOnlyCommandMetadata metadata, Object context)
     {
         Dialog.alert("Executing command for " + context.toString());
     }           
 }
 

Creating menu items by subclassing and implementing Runnable

If subclassing the extending class must implement the Runnable interface, which in turn supports abstract dispatching of menu actions on activation.

    ...
    // setup the menu items
    MenuItem item = new MyMenuItem();
    menu.addItem(item);
    ...
    class MyMenuItem extends MenuItem {
        MyMenuItem() {
            super(MyResourceBundle.getBundle(), MyResource.MY_MENU_ITEM, 0, 100);
        }
        public void run() {
            // do something
        }
    }
 

[/QUOTE]

BlackBerry Java JDK 6.0.0.30
BlackBerry Java Plug-in 1.3.0.201101051005-15
BlackBerry JRE 6.0.0
Eclipse: Helios

Please use plain text.
Developer
jprofitt
Posts: 604
Registered: ‎12-27-2010
My Device: Not Specified

Re: ?? why "MenuItem(String, int, int)" deprecated in 1.3?

With regard to the deprecation, the documentation online says to use MenuItem(StringProvider, int, int).

Please use plain text.
Regular Contributor
zumwaltwood
Posts: 81
Registered: ‎01-14-2011
My Device: Curve 8530

Re: ?? why "MenuItem(String, int, int)" deprecated in 1.3?

So how is:

MenuItem(String, int, int)

 

Really any different than

MenuItem(StringProvider, int, int)

 

Seriously, whats the point in doing

StringProvider sendExpenses = new StringProvider("Send Expenses");
private MenuItem _sendExpenses = new MenuItem(sendExpenses,110,11)
{
  public void run()
  {
    Dialog.inform("Send Expenses to Server");
  }
};

 

Vs doing

private MenuItem _sendExpenses = new MenuItem("Send Expenses",110,11)
{
  public void run()
  {
    Dialog.inform("Send Expenses to Server");
  }
};

 

 

BlackBerry Java JDK 6.0.0.30
BlackBerry Java Plug-in 1.3.0.201101051005-15
BlackBerry JRE 6.0.0
Eclipse: Helios

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.634 Dev OS, Z30 (STA100-5)-10.3.1.634 Dev OS, Passport (SQW100-1)-10.3.0.1154, PlayBook (16GB)-2.1.0.1917

Re: ?? why "MenuItem(String, int, int)" deprecated in 1.3?

From StringProvider: "Provides a String that can be changed. The most common use case would be for locale changes." If you hard code the Strings then it's pointless, if you use a resource bundle then it's great because you simply provide the Resource family and ID at creation and the rest is handled internally.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Regular Contributor
zumwaltwood
Posts: 81
Registered: ‎01-14-2011
My Device: Curve 8530

Re: ?? why "MenuItem(String, int, int)" deprecated in 1.3?

Oh, ok, well, have not used resource bundles as of yet I do not think, that or I do not understand fully what a resource bundle is for, that will come in time, still new at blackberry dev work. I am trying to keep the use of packages to a minimal since this is a phone app, and having to include yet another import package seems like a waste of memory to me. I just haven't seen a need for the change yet, it will eventually happen though, I am sure. Thanks

 

BlackBerry Java JDK 6.0.0.30
BlackBerry Java Plug-in 1.3.0.201101051005-15
BlackBerry JRE 6.0.0
Eclipse: Helios

Please use plain text.
New Contributor
im4k
Posts: 5
Registered: ‎06-16-2011
My Device: Blackberry Bold 9780

Re: ?? why "MenuItem(String, int, int)" deprecated in 1.3?

Thankfully you just ran into this issue, because as someone learning Java for the first time, the 2nd tutorial you run across this, and I have spent hours trying to fix it.

 

Your posting helped me figure out (even as a noob)  what the heck was going wrong.

 

I hope Andre updates his tutorials soon, I've run into several non-noob friendly issues. :robotsad:

 

The code in question is this (it's in the User Interface tutorial):

private MenuItem _changeCapital = new MenuItem("Change Capital", 110, 10) {
	  public void run() {
   	  if (displayed == 0)
		    _canadaCapital = _input.getText();
	  else if (displayed == 1)
		    _ukCapital = _input.getText();
	  else if (displayed == 2)
		    _usCapital = _input.getText();
	  }
	  };
	  
	  private MenuItem _close = new MenuItem("Close", 110, 10) { public void run() {
		onClose();
	  }
	  };

 

 Needs to look like this (changes in Bold):

StringProvider changeCapital = new StringProvider("Change Capital");
      private MenuItem _changeCapital = new MenuItem(changeCapital, 110, 10) {
      public void run() {
      if (displayed == 0)
    	    _canadaCapital = _input.getText();
      else if (displayed == 1)
    		_ukCapital = _input.getText();
   	  else if (displayed == 2)
    		_usCapital = _input.getText();
      }
      };
      
      StringProvider close = new StringProvider("Close"); private MenuItem _close = new MenuItem(close, 110, 10) { public void run() {
    	    onClose();
      }
      };

 

 

 

Please use plain text.
New Contributor
beluluk
Posts: 2
Registered: ‎09-12-2011
My Device: BlackBerry 9300

Re: ?? why "MenuItem(String, int, int)" deprecated in 1.3?

Hi... I am a newbie and just hit this post on coincidence. Please excuse me if I am wrong. But... if you said that "StringProvider privides a String that can be changed", should we not use the method setText() for that purpose?

 

Thanks


Fandi.

Please use plain text.
Contributor
ashutosh_porchys
Posts: 29
Registered: ‎04-01-2011
My Device: 9800

Re: ?? why "MenuItem(String, int, int)" deprecated in 1.3?

Hi,

      can any one please Tell me that how can I get the Index of menuItem rather than String.

 

I am Pasting My code here so that you can uderstand that what i actually want..

 

 

 

package com.rim.samples.local;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;

class LocalScreen extends MainScreen
{
Menu menu;
public LocalScreen()
{

}
private MenuItem LoginMenuItem = new MenuItem("one", 10, 1)
{
    public void run()
    {
       
    }
};

private MenuItem ATMMenuItem = new MenuItem("two", 10, 2) {
    public void run() {
    }
};

private MenuItem ExitMenuItem = new MenuItem("Three", 10, 3)
{
    public void run() {
    }
};   

protected void makeMenu(Menu menu, int instance)
{
    menu.add(LoginMenuItem);
    menu.add(ATMMenuItem);
    menu.add(ExitMenuItem);
    super.makeMenu(menu, instance);
}

protected boolean keyCharUnhandled(char key, int status, int time)
{
    boolean retval = false;
    switch (key)
    {
    case Characters.CONTROL_MENU:
        System.out.println("here");
        menu = new Menu();
        makeMenu(menu, 0);
        int i = menu.show();
        if (i == -1)
        {
            return false;
        }
       
       /*String strSelectedMenu = menu.getSelectedItem().toString();

         if(strSelectedMenu.toLowerCase().equals("one"))
            {
                Dialog.alert("That can be done easly");
            }*/

 

 

        But here I want to do any Action from selecting Index,Because This code I will use in localization where we can not compare it with string ..like arabic french etc..So please help me any one..

        //int indexSelectedMenu=menu.getSelectedItem().getId();
        int indexSelectedMenu=menu.getSelectedItem().getPriority();       
        Dialog.alert(""+indexSelectedMenu);       
      

 

 

        if (indexSelectedMenu==1)
        {
            Dialog.alert("This is One");
        }
        else if (indexSelectedMenu==2)
        {
            Dialog.alert("This is Two");
        }
        else if (indexSelectedMenu==3)
        {
            Dialog.alert("This is Three");
        }
       
       
       
    }
    return retval;
}
}

 

 

Thanks in Advance.

Thanks
With Best Regards
Ashutosh Srivatava
Please use plain text.
New Contributor
beluluk
Posts: 2
Registered: ‎09-12-2011
My Device: BlackBerry 9300

Re: ?? why "MenuItem(String, int, int)" deprecated in 1.3?

I'm just a newbie... but can i ask you why not do:

private MenuItem LoginMenuItem = new MenuItem("one", 10, 1)
{
    public void run()
    {
            Dialog.alert("This is One");
    }
};

private MenuItem ATMMenuItem = new MenuItem("two", 10, 2) {
    public void run() {

            Dialog.alert("This is Two");
    }
};

private MenuItem ExitMenuItem = new MenuItem("Three", 10, 3)
{
    public void run() {

            Dialog.alert("This is Three");
    }
};   

 

 

instead of:

        if (indexSelectedMenu==1)
        {
            Dialog.alert("This is One");
        }
        else if (indexSelectedMenu==2)
        {
            Dialog.alert("This is Two");
        }
        else if (indexSelectedMenu==3)
        {
            Dialog.alert("This is Three");
        }

Please use plain text.
New Contributor
Ei8ht
Posts: 6
Registered: ‎06-06-2011
My Device: 9700

Re: ?? why "MenuItem(String, int, int)" deprecated in 1.3?

Looks like the StringProvider is not meant for OS 5.x. Giving the java.lang.NoClassDefFound err. But, this is a great tip.:Yes:

Please use plain text.