02-10-2012 03:37 AM
Hello,
My application has one class myscreen that extends MainScreen. So as user starts using the application I just update myscreen ( so I don't use popscreen or pushscreen nor do I use a new instance of any screen)
So my Menu is the same over all the application.
My question is can I remove/add MenuItems ? I know how add/remove MenuItem if I am using different Screens, however my application has only one screen.
How can I get an instance of the "Menu" so that I can use Menu.add(menuItem) Menu.delete(MenuItemIndex) from my functions (my functions are in synchronized blocks inside a timerTask)
I hope I am clear, so that anyone could help...
Thanks in advance ...
02-10-2012 05:52 AM
if possible then send the code.
Thanks.
02-10-2012 06:05 AM
Solved...
I over ridden makeMenu and added to it some dynamic static variables that I change as my application change (with needs) ...
-----------working code-----------------------
static boolean d_FromMainpage=true;
protected void makeMenu(Menu menu , int instance) {
//d_FromMainpage changes as my application goes on ... (so as I please I toggle between adding each of the items)
// note that -makeMenu()- is called each while or has a auto-change-listen ... I am really not sure about any of this , //what I am sure about is that this CODE worked for me. Hope I helped.
menu.deleteAll();
if(d_FromMainpage)
menu.add(RestartItem);
else menu.add(StartItem);
menu.add(RateUs);
menu.add(MenuItem.separator(122));
menu.add(Help);
menu.add(MenuItem.separator(155));
menu.add(exitItem);
}
02-11-2012 07:15 AM - last edited on 02-11-2012 07:17 AM
This is a sample Code gives how to add and remove the menuItem from the Menu;
According to this you can you can add or remove your menuItems:
The below is sample class: Abc.java
public class Abc extends MainScreen implements FieldChangeListener
{
String menuItemName="Add or Remove";
ButtonField addMenu,removeMenu;
MenuItem menuItem;
public Abc()
{
createGUI();
}
public void createGUI()
{
addMenu=new ButtonField("Add Menu");
addMenu.setChangeListener(this);
add(addMenu);
removeMenu=new ButtonField("Remove Menu");
removeMenu.setChangeListener(this);
add(removeMenu);
menuItem=new MenuItem(menuItemName,100,101)
{
public void run()
{
Dialog.alert("Add or Remove Menu Item");
}
};
addMenuItem(menuItem);
}
public void fieldChanged(Field field, int context)
{
if(field==addMenu)
{
if(AddOrRemoveMenuItems.addTheMenu(menuItem))
{
Dialog.alert("Aleady Added to MenuItem");
}
else
{
addMenuItem(menuItem);
Dialog.alert("Added to MenuItem Successfully");
}
}
else if(field==removeMenu)
{
if(AddOrRemoveMenuItems.removeTheMenu(menuItem))
{
removeMenuItem(menuItem);
Dialog.alert("MenuItem Removed Successfully");
}
else
{
Dialog.alert("Already Removed from MenuItem");
}
}
}
}
I took both addTheMenu(menuItem) and removeTheMenu(menuItem) methods in separate class. So I can call anywhere in my application;
AddOrRemoveMenuItems.java
public class AddOrRemoveMenuItems
{
public static boolean addTheMenu(MenuItem menuItem)
{
boolean isAdding=false;
Screen screen=Ui.getUiEngine().getActiveScreen();
Menu menu=screen.getMenu(0);
for(int i=0;i<menu.getSize();i++)
{
if(menu.getItem(i).toString().equalsIgnoreCase(men uItem.toString()))
{
isAdding=true;
}
}
return isAdding;
}
public static boolean removeTheMenu(MenuItem menuItem)
{
boolean isRemoving=false;
Screen screen=Ui.getUiEngine().getActiveScreen();
Menu menu=screen.getMenu(0);
for(int i=0;i<menu.getSize();i++)
{
if(menu.getItem(i).toString().equalsIgnoreCase(men uItem.toString()))
{
isRemoving=true;
}
}
return isRemoving;
}
}
Check this sample code first and then use this according to your requirement;
=================================================
Feel to click LIKE button if the solution helps you;