开发者

GXT Button shows menu on first click, won't hide menu on second click

Is this the intended functionality or am I doing something wrong?

All I'm doing is creating a GXT Button and calling setMenu to attach a GXT menu. On first click, the menu shows properly, on second click, the menu disappears on MouseDown, but reappears on MouseUp. The only way to get the menu to hide is to click away from the button.

I confirmed that it isn't开发者_开发技巧 anything strange with a particular button in my code by adding another button:

Button button = new Button("test");
Menu menu = new Menu();
button.setMenu(menu);
add(button);

If this is intended, is there a suggestion on how to add a listener to close the menu on second click?


I am guessing that it is working as intended since the menu always hides as soon as it loses focus. What I did below is override the onAutoHide method in the menu to not hide if the button with the specified ID is pressed (change accordingly). This gives me the ability to check if the menu is shown in the onClick method of the button - and then not show it again. Be warned though...I am in no way an expert and this is a hack :)

Button button = new Button("Test") {
        @Override
        protected void onClick(ComponentEvent ce) {
            ce.preventDefault();
            focus();
            hideToolTip();
            if (!disabled) {
                ButtonEvent be = new ButtonEvent(this);
                if (!fireEvent(Events.BeforeSelect, be)) {
                    return;
                }
                if (menu != null) {
                    if (!menu.isVisible())
                        showMenu();
                    else
                        hideMenu();
                }
                fireEvent(Events.Select, be);
            }
        }
    };
    button.setId("TESTBUTTONID");
    Menu menu = new Menu() {
        @Override
        protected boolean onAutoHide(PreviewEvent pe) {
            if (pe.getEventTypeInt() == Event.ONMOUSEDOWN
                    && !(pe.within(getElement()) || (fly(pe.getTarget())
                            .findParent(".x-ignore", -1) != null))
                    && !(fly(pe.getTarget()).findParent(".x-btn", -1) != null
                    && fly(pe.getTarget()).findParent(".x-btn", -1).getId()
                            .equalsIgnoreCase("TESTBUTTONID"))) {
                MenuEvent me = new MenuEvent(this);
                me.setEvent(pe.getEvent());
                if (fireEvent(Events.AutoHide, me)) {
                    hide(true);
                    return true;
                }
            }
            return false;
        }
    };
    button.setMenu(menu);
    RootPanel.get().add(button);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜