Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    how to use menuItem.checkIf

    Hi,

    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?


    thank you

    #2
    You should use MenuItem.setSetCheckIfCondition.

    (MenuItem.setCheckIf doesn't exist in SVN)

    You can find an example usage in this sample.

    Sanjiv
    Last edited by sjivan; 14 Jan 2009, 04:48.

    Comment


      #3
      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:

      Code:
               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);
      Last edited by la123; 18 Jan 2009, 02:19.

      Comment


        #4
        hi folks,

        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

        i could cry because of such things...

        Comment


          #5
          You haven't posted the version of Smart GWT you're using. Try using the latest nightly build to see if you're still seeing the issue.

          Comment


            #6
            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.

            Comment


              #7
              @mhulsman

              thank you, but be sure, that i know the difference between enabled and checked...

              the point is, the method "setCheckIfCondition" does not work properly, the syme for "setEnableIfCondition"

              i guess, the handling of that methods is pretty the same, thats why i posted in the syme topic...

              @smartgwt.dev: i'm using the smartgwt version 2.2 for now, will try with the latest nightly... and post results here

              cheers andre

              Comment


                #8
                SmartGWT 2.2

                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.

                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

                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
                Code:
                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)

                Comment


                  #9
                  Please try the latest nightly build and see if the error still occurs.

                  Sanjiv

                  Comment


                    #10
                    I have downloaded the latest build.

                    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.

                    Kind regards.

                    Comment

                    Working...
                    X