I need to check / uncheck a menu item in runtime, based on user selection.
I saw a function menuItem.checkIf(Canvas, Menu, MenuItem)
I don’t understand how to use it: where and how I specify a condition to disable/enable menuItem? What should be the value of canvas?
I need to deselect all the records in menu.
Can’t I use this method in runtime:
menuButton.getMenu().deselectAllRecords()?
Also, I see that MenuItem.setSetCheckIfCondition works only if I set menus using MenuItem.setItems
It doesn’t work if I set them one by one, using MenuItem.addItem (I use your build 12-18-2008).
Here is my code for example:
final ComboBoxItem cbItem = new ComboBoxItem();
cbItem.setTitle("Select");
cbItem.setType("comboBox");
cbItem.setValueMap("Cat", "Dog", "Giraffe", "Goat", "Marmoset", "Mouse");
form.setFields(cbItem);
form.setTop(180);
canvas.addChild(form);
Menu sizeMenu = new Menu();
MenuItem smallItem = new MenuItem("Small");
smallItem.setCheckIfCondition(new MenuItemIfFunction() {
public boolean execute(Canvas target, Menu menu, MenuItem item) {
return cbItem.getValue().equals("Dog");
}
});
sizeMenu.addItem(smallItem);
MenuItem mediumItem = new MenuItem("Medium");
mediumItem.setCheckIfCondition(new MenuItemIfFunction() {
public boolean execute(Canvas target, Menu menu, MenuItem item) {
return cbItem.getValue().equals("Mouse");
}
});
sizeMenu.addItem(mediumItem);
MenuItem sizeItem = new MenuItem("Size");
sizeItem.setSubmenu(sizeMenu);
MenuButton mainMenuButton = new MenuButton("size menu", sizeMenu);
mainMenuButton.setWidth(150);
canvas.addChild(mainMenuButton);
having the same issue
i need to “disable” a MenuItem dynamically, setSetCheckIfCondition does not work for me, using addItem method for adding menu items to the menu
If you want to disable a MenuItem, setCheckIfCondition is not what you want to use. This function just puts a check icon next to the MenuItem. You want to use setEnableIfCondition() and return false if you want it to be disabled.
I have problems too.
I need a menu (integrated in a menubar) where just one of two menu items is checked. I started with this code.
Menu menu = new Menu();
menu.setTitle("Menu");
MenuItem item1 = new MenuItem("Item 1");
item1.setChecked(true) ;
item1.addClickHandler( new ClickHandler() {
@Override
public void onClick(MenuItemClickEvent event) {
if (!item1.getChecked()) {
item1.setChecked(true) ;
item2.setChecked(false) ;
}
// do actions related to item1
}
});
MenuItem item2 = new MenuItem("Item 2");
item2.addClickHandler( new ClickHandler() {
@Override
public void onClick(MenuItemClickEvent event) {
if (!item2.getChecked()) {
item2.setChecked(true) ;
item1.setChecked(false) ;
}
// do actions related to item2
}
});
All is working except that the visual check of the menu items is not toggled (not a detail!).
Having stumbled on this thread, I decided to use setCheckIfCondition to toggle the visual check of the menu items. Here is the code
enum Status {ITEM1, ITEM2}
Status status = Status.ITEM1 ;
Menu menu = new Menu();
menu.setTitle("Menu");
MenuItem item1 = new MenuItem("Item 1");
item1.setCheckIfCondition(new MenuItemIfFunction() {
@Override
public boolean execute(Canvas target, Menu menu, MenuItem item) {
return status == Status.ITEM1 ;
}
});
item1.addClickHandler( new ClickHandler() {
@Override
public void onClick(MenuItemClickEvent event) {
status = Status.ITEM1 ;
// do actions related to item1
}
});
MenuItem item2 = new MenuItem("Item 2");
item2.setCheckIfCondition(new MenuItemIfFunction() {
@Override
public boolean execute(Canvas target, Menu menu, MenuItem item) {
return status == Status.ITEM2 ;
}
});
item2.addClickHandler( new ClickHandler() {
@Override
public void onClick(MenuItemClickEvent event) {
status = Status.ITEM1 ;
// do actions related to item2
}
});
menu.setItems(item1,item2);
Now it is even worse. When I click on the “Menu” in the menubar I got 4 errors, each looking like
10:33:13.326 [ERROR] [trainingstatistics] Uncaught exception escaped
com.google.gwt.core.client.JavaScriptException: (TypeError): 'null' is null or not an object
number: -2146823281
description: 'null' is null or not an object
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:195)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:120)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:507)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:264)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:188)
at sun.reflect.GeneratedMethodAccessor16.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
at com.google.gwt.dev.shell.BrowserChannel.reactToMessages(BrowserChannel.java:1668)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:401)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:222)
at java.lang.Thread.run(Thread.java:619)
The version of my code using setCheckIfCondition works correctly now.
The other version remains with the problem that the visual check of the menuiten remains fixed on the first menuiten (item1). It is only a visual problem, because if I click on the second menuiten (item2) and then check with item2.getChecked() it returns true (even if the visual check remains on item1). I hope you will fix soon this problem.