Question

I am using GWT 2.5.1

I have been struggling with this for hours now. How do you disable and/or hide a MenuItem in GWT.

menuItem.setEnabled(false); // DOES NOT WORK !!!
menuItem.setVisible(false); // DOES NOT WORK !!!
UIObject.setVisible(menuItem.getElement(), false); // DOES NOT WORK !!!
menuItem.setScheduledCommand(null); // DOES NOT WORK !!!
Was it helpful?

Solution

Hide/Show item: You can remove a MenuItem with MenuBar.removeItem(MenuItem). If you want to show it again, you can use MenuBar.insertItem(MenuItem, index).

  • [Update] I also tried menuItem.setVisible(false) and it works as expected (item is hidden).

Disable item: I'm not sure, why menuItem.setEnabled(false) doesn't work for you.

Take a look at the following example. I tested it with GWT 2.5.1 and it works.

FlowPanel panel = new FlowPanel();
final MenuBar menubar = new MenuBar();
final MenuItem item = menubar.addItem("item one", new ScheduledCommand() {

    @Override
    public void execute() {
        Window.alert("item one clicked");

    }
});
menubar.addItem("item two", new ScheduledCommand() {

    @Override
    public void execute() {
        Window.alert("item two clicked");

    }
});

panel.add(menubar);

// Disable execution of command
Button buttonDisable = new Button("Disable item");
buttonDisable.addClickHandler(new ClickHandler() {

    @Override
    public void onClick(ClickEvent event) {
        item.setEnabled(false);
    }
});
panel.add(buttonDisable);

// Hide/Remove the item from the menubar
Button buttonHide = new Button("Hide item");
buttonHide.addClickHandler(new ClickHandler() {

    @Override
    public void onClick(ClickEvent event) {
        menubar.removeItem(item);

    }
});
panel.add(buttonHide);
RootPanel.get().add(panel);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top