How can I add menus to my black berry application
Assuming your extending the MainScreen it's quite easy! First you'd need to create MenuItem's, this is done like so:
private MenuItem _menuItem1 = new MenuItem("Menu Item 1", 10, 10){
public void run(){
// insert code thats to be done when menu is selected here here
}
};
private MenuItem _menuItem2 = new MenuItem("Menu Item 2", 10, 10){
public void run(){
// insert code thats to be done when menu is selected here here
}
};
Then you'd need to add these to your like which is done like so:
protected void makeMenu(Menu menu, int instance){
menu.add(_menuItem1);
menu.addSeparator();
menu.add(_menuItem2);
}
BlackBerry support forums:
Add a custom menu item to an existing BlackBerry application
1.create menu item which you want to add on menu
MenuItem menuItemBack = new MenuItem(new StringProvider("back"), 110, 10) {
public void run() {
Dialog.alert("back");
}
};
MenuItem menuItemFwd = new MenuItem(new StringProvider("Forword"), 110, 10) {
public void run() {
Dialog.alert("forword");
}
};
2.after that you can add these item on menu by using
1.add these item in to Mainscreen constructor.
addMenuItem(menuItemBack);
addMenuItem(menuItemBack);
addMenuItem(MenuItem.separator(110));
addMenuItem(menuItemFwd);
addMenuItem(MenuItem.separator(110));
you can create your custom menu by overriding the makeMenu() method
protected void makeMenu(Menu menu, int instance) {
menu.addMenuItem(menuItemBack);
menu.addMenuItem(menuItemFwd);
};
Related
I am creating a Dropdown list which should have CREATE, DELETE, SUBMIT for a particular row in a table. Can someone help me how to create one in GWT.
table.addCellPreviewHandler(new Handler<RequestDto>()
{
#Override
public void onCellPreview(CellPreviewEvent<RequestDto> event)
{
if (event.getNativeEvent().getButton() == NativeEvent.BUTTON_RIGHT)
{
MenuBar options = new MenuBar();
MenuBar gwtPop = new MenuBar();
options.addItem("Create", gwtPop);
options.addItem("Submit", gwtPop);
MenuItem Import = new MenuItem(new SafeHtmlBuilder().appendEscaped("Import").toSafeHtml());
Import.setScheduledCommand(new ScheduledCommand()
{
#Override
public void execute()
{
Window.alert("hello");
}
});
final DialogBox menuWrapper = new DialogBox(true);
menuWrapper.add(options);
gwtPop.addItem(Import);
First of all, do not construct your menu inside onCellPreview method. There is no need to build the same widget over and over again on each click.
You can do something like this:
int clickedRow = -1;
...
// build your menu here
// use clickedRow where necessary, for example:
deleteMenuItem.setScheduledCommand(new ScheduledCommand() {
#Override
public void execute() {
Window.alert("hello, I am about to delete row " + clickedRow);
}
});
...
myTable.addCellPreviewHandler(new Handler<MyObject>() {
#Override
public void onCellPreview(CellPreviewEvent<MyObject> event) {
if (event.getNativeEvent().getButton() == NativeEvent.BUTTON_RIGHT) {
event.getNativeEvent().stopPropagation();
clickedRow = event.getIndex();
// show your menu - no need to construct it again
}
}
I have a bitmap field in my blackberry5 app with fieldChanged listener attached to it which works absolutely fine
now my problem is that I also have an associated menu for the same purpose (I can not remove it's the requirement) and on click the menu I get a JVM 104 IllegalStateException
here is my menu class
public class TabMenu extends MenuItem{
MainScreen menuScreen;
Field button;
public TabMenu(String menuLabel,MainScreen menuScreen,Field button)
{
super(menuLabel, 1, 0);
this.menuScreen = menuScreen;
this.button = button;
}//end constructor
public void run()
{
FieldChangeListener listener = (FieldChangeListener)this.menuScreen;
listener.fieldChanged(this.button, this.button.getIndex());
this.button.setFocus();
}
}
and here is menu and fieldchnaged code
protected void makeMenu(Menu menu, int instance) {
menu.add(new RefreshMenu());
menu.addSeparator();
menu.add(new TabMenu("Go >", this, goTab));
menu.addSeparator();
}
public void fieldChanged(Field field, int context) {
if (field == goTab) {
Dialog.alert("goinf")
}
}
Try changing your TabMenu#run() method to the following:
public void run() {
this.button.fieldChangedNotify(this.button.getIndex());
this.button.setFocus();
}
I have mainscreen with 5 menus and after a certain action I need to delete 2 of them in a method that peforms the action, can I delete menu items programetically ?
Run this sample code:
public class Abc extends MainScreen
{
ButtonField click;
MenuItem saveMenu;
public Abc()
{
createGUI();
}
private void createGUI()
{
saveMenu=new MenuItem("Save", 100, 101);
addMenuItem(saveMenu);
click=new ButtonField("click");
click.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
removeTheMenu(saveMenu);
}
});
add(click);
}
public void removeTheMenu(MenuItem menuItem)
{
Screen screen=Ui.getUiEngine().getActiveScreen();
Menu menu=screen.getMenu(0);//Gives the Menu list of active screen only;
for(int i=0;i<menu.getSize();i++)
{
if(menu.getItem(i).toString().equalsIgnoreCase(menuItem.toString()))
{
removeMenuItem(menuItem);
Status.show("Removed Successfully", 1000);
}
}
}
}
This code may help you;
I created 6 menu items in the menu for the default launching screen and there is one option in the menu which is mandatory to launch before clicking on any other menu option. And it is not possible to display that particular as a default screen (due to the parameters in my project). So i want to disable all the other menu options so that the user is forced to click only that particular option. Is this possible? Please help me. Thank you
What do you mean by disabled? Should it looked disabled, or just be disabled? You can use a class variable to make sure nothing else runs until you say it can. In your makeMenu function, do something like this:
private boolean goAhead = false;
protected void makeMenu(Menu menu, int instance)
{
menu.add(new MenuItem("Menu Item Mandatory Thing", 1, 1){
public void run()
{
//do stuff
goAhead = true;
}
});;
menu.add(new MenuItem("Menu Item 2", 2, 2){
public void run()
{
if(goAhead)
{
//do stuff
}
}
});
menu.add(new MenuItem("Menu Item 3", 3, 3){
public void run()
{
if(goAhead)
{
//do stuff
}
}
});
}
Hello fellow BB programmers,
if you look at the native Contacts (Addressbook) application on a Blackberry 6 phone with touch screen - it has a very natural behaviour:
On a short tap the default action is being executed - viewing the selected addressbook entry
On a long touch and hold a menu with several actions is displayed:
I'm trying to create an app with a ListField and similar (and intuitive) behaviour myself: run default action on a short tap and display a menu in the middle of screen with several secondary actions on a longer touch.
I've searched a lot and unfortunately only managed to create a test app with exactly opposite behaviour sofar:
I listen for a TouchGesture.HOVER and run editMenu.run(). And for the short tap a menu comes by itself (I haven't found yet, what makes it appear, some method in MainScreen/Screen?). I've tried running onMenu(0) but the menu appears in the top/right corner instead of screen center.
Below is my very simple test code MyList.java, please help me to fix it:
package mypackage;
import java.util.*;
import net.rim.device.api.collection.*;
import net.rim.device.api.collection.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.util.*;
public class MyList extends UiApplication {
public static void main(String args[]) {
MyList app = new MyList();
app.enterEventDispatcher();
}
public MyList() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
ObjectListField myList = new ObjectListField() {
protected boolean touchEvent(TouchEvent event) {
if (event.getEvent() == TouchEvent.GESTURE) {
TouchGesture gesture = event.getGesture();
if (gesture.getEvent() == TouchGesture.HOVER) {
System.err.println("XXX hover=" + gesture.getHoverCount() + ", index=" + myList.getSelectedIndex());
editMenu.run();
// onMenu(0);
return true;
}
}
return super.touchEvent(event);
}
};
private final MenuItem addMenu = new MenuItem("Add item", 0, 0) {
public void run() {
Status.show("Adding new item");
}
};
private final MenuItem editMenu = new MenuItem("Edit item", 1, 0) {
public void run() {
Status.show("Editing existing item: " + myList.getSelectedIndex());
}
};
private final MenuItem removeMenu = new MenuItem("Remove item", 2, 0) {
public void run() {
Status.show("Removing existing item: " + myList.getSelectedIndex());
}
};
public MyScreen() {
setTitle("How to display menu on long click?");
myList.set(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", });
add(myList);
addMenuItem(addMenu);
addMenuItem(editMenu);
addMenuItem(removeMenu);
}
}
Thank you!
Alex
The issue described here is related to the new Pop-up menus introduced in the OS 6. Using TouchEvent is a hack and will not work for all devices (not all OS 6 devices have a touch-screen).
class MyScreen extends MainScreen {
ObjectListField myList = new ObjectListField() {
protected boolean navigationClick(int status, int time) {
editMenu.run();
return true;
}
};
private final MenuItem addMenu = new MenuItem("Add item", 0, 0) {
public void run() {
Status.show("Adding new item");
}
};
private final MenuItem editMenu = new MenuItem("Edit item", 1, 0) {
public void run() {
Status.show("Editing existing item: " + myList.getSelectedIndex());
}
};
private final MenuItem removeMenu = new MenuItem("Remove item", 2, 0) {
public void run() {
Status.show("Removing existing item: " + myList.getSelectedIndex());
}
};
public MyScreen() {
setTitle("How to display menu on long click?");
myList.set(new String[] { "Item 1", "Item 2", "Item 3", "Item 4", });
add(myList);
addMenuItem(addMenu);
addMenuItem(editMenu);
addMenuItem(removeMenu);
}
}
Why this works as expected? Adding menu items to the screen implicitly sets a ContextMenuProvider for the screen (it defines a strategy for displaying a screen's pop-up menu). So the hover works as expected at a screen level - it is the screen who detects the "hover event" and opens the pop-up menu. On the other hand "taps" are handled with the list in navigationClick().