Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TabLayoutPanel disable a Tab GWT

How can i disable a tab (i.e the user cannot open the tab when he clicks on it) in the TabLayoutPanel?I searched online but was not able to find a solution

Thanks

like image 968
Barry Avatar asked Feb 25 '11 21:02

Barry


2 Answers

Use a BeforeSelectionHandler:

TabLayoutPanel myPanel = new TabLayoutPanel();
// Add children...

myPanel.addBeforeSelectionHandler(new BeforeSelectionHandler<Integer>() {
  @Override
  public void onBeforeSelection(BeforeSelectionEvent<Integer> event) {
    // Simple if statement - your test for whether the tab should be disabled
    // will probably be more complicated
    if (event.getItem() == 1) {
      // Canceling the event prevents the tab from being selected.
      event.cancel();
    }
  }
});

If you want to style the disabled tab differently than enabled tabs, you can use TabLayoutPanel#getTabWidget to get the tab widget and add a style name to it.

like image 123
Jason Terk Avatar answered Oct 17 '22 03:10

Jason Terk


For anyone who comes across this later:

As of GWT version 1.6, disabling/enabling tabs is built into GWT. The TabBar class has a method setTabEnabled(int index, boolean enabled) that enables/disables the tab at a given index.

For example, to disable all the tabs in a TabPanel:

TabPanel myTabPanel = new TabPanel();
// Add children

TabBar tabBar = myTabPanel.getTabBar();
for(int i=0; i<tabBar.getTabCount(); i++) {
    tabBar.setTabEnabled(i, false);
}

See the GWT javadoc for more info.

To style disabled tabs differently (which GWT does automatically, but if you wanted to change the style): disabled tabBarItem divs are given another CSS class: gwt-TabBarItem-disabled.

like image 27
Adam Nellis Avatar answered Oct 17 '22 02:10

Adam Nellis