Announcement

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

    Change MenuItem Text, how to?

    Hi,

    Is there an example how to change the text of a menu item every time the menu was clicked.

    I already tried the setTitle method in the onClick method of the ClickHandler but the text won't change. I also tried setDynamicTitleFunction but the execute method was never called.

    Thanks for your help.

    #2
    setDynamicTitleFunction is the right approach for this. Can you show the code that tries this approach but isn't working for you?

    Comment


      #3
      This is the part where I create the MenuItem and set the setDynamicTitleFunction method. I only see the text that I use in the Constructor of MenuItem, the execute methode is never called.

      Maybe I have a probelm understanding the mechanism behind this. When and from where is the execute method of the MenuItemStringFunction called?

      The text should change everytime the onClick Method of the ClickHandler is called.

      Code:
      final MenuItem activateMenu = new MenuItem( "Activate" );
      activateMenu.setDynamicTitleFunction( new MenuItemStringFunction() {
      
      	public String execute( final Canvas aTarget, final Menu aMenu, final MenuItem aItem ) {
      
      		System.out.println( "execute called" );
      		if ( ddiVh.isUseBlacklist() ) {
      			return "De-Activate Blacklist";
      		}
      		else {
      			return "Activate Blacklist";
      		}
      	}
      
      } );
      
      activateMenu.addClickHandler( new ClickHandler() {
      
      	public void onClick( final MenuItemClickEvent event ) {
      		processStateChangeEvent();
      	}
      } );

      Comment


        #4
        I tried this and it worked fine. I added the menu item below to this sample :

        http://www.smartclient.com/smartgwt/...ory_appearance

        and each time I click the "File" MenuButton, the activateMenu's dynamic function is invoked as expected and the String returned is the new MenuItem title.

        I tried this on the latest build but nothing related to this has changed since 1.0b1. Which build are you using?

        If you're still having troubles, please post a simple standalone testcase.

        Thanks,
        Sanjiv

        Code:
        final MenuItem activateMenu = new MenuItem("Activate");
        activateMenu.setDynamicTitleFunction(new MenuItemStringFunction() {
        
            public String execute(final Canvas aTarget, final Menu aMenu, final MenuItem aItem) {
        
                System.out.println("execute called");
                if (Math.random() > 0.5) {
                    return "De-Activate Blacklist";
                } else {
                    return "Activate Blacklist";
                }
            }
        });

        Comment


          #5
          I can confirm this bug too. Sorry I don't have any sample code to post but I suspect it has something to do with menuitems that are added to the menu dynamically (i.e. for example as a result of user action).

          I tested this with a menu which is initialized normally and made visible and it works (as in showcase). But I have some code that fetches data from server with datasource and manually builds proper menuitems from the data returned from the server and after this this method is never called. Even if it's identically set as in the former case.

          This with gwt 2.0.4 + smartgwt lgpl 2.3 2010-10-08 nightly build.

          Comment


            #6
            You need to post a standalone testcase if you think you've found a bug.

            Comment


              #7
              Adapted straight from here http://forums.smartclient.com/showthread.php?t=9648

              As you can see setDynamicIconFunction(...) fails too. Probably the same cause whatever it was as in the (above linked) checkedifcondition-function.

              Code:
                      VLayout mainLayout = new VLayout();
              
                      final Menu menu = new Menu();
                      menu.setShowShadow(true);
                      menu.setShadowDepth(10);
              
                      MenuItem works = new MenuItem("This gets overrided", "icons/bug.png");
                      works.setDynamicTitleFunction(new MenuItemStringFunction() {
                          public String execute(Canvas canvas, Menu menu, MenuItem menuItem) {
                              return "This works";
                          }
                      });
                      works.setDynamicIconFunction(new MenuItemStringFunction() {
                          public String execute(Canvas canvas, Menu menu, MenuItem menuItem) {
                              return "icons/comment.png";
                          }
                      });
              
                      menu.addItem(works);
              
                      new Timer() {
                          @Override
                          public void run() {
                              MenuItem fails = new MenuItem("This fails", "icons/bug.png");
                              fails.setDynamicTitleFunction(new MenuItemStringFunction() {
                                  public String execute(Canvas canvas, Menu menu, MenuItem menuItem) {
                                      return "Should be shown";
                                  }
                              });
                              fails.setDynamicIconFunction(new MenuItemStringFunction() {
                                  public String execute(Canvas canvas, Menu menu, MenuItem menuItem) {
                                      return "icons/comment.png";
                                  }
                              });
                              menu.addItem(fails);
                          }
                      }.schedule(3000);
              
                      MenuButton mb = new MenuButton("Menu", menu);
              
                      mainLayout.addMember(mb);
                      mainLayout.draw();

              Comment


                #8
                Any news on this ?

                Comment


                  #9
                  Fixed now. The fix will show up in the next nightly build.

                  Comment

                  Working...
                  X