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 w开发者_开发百科as not able to find a solution
Thanks
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.
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
.
You can access tab style by casting class Tab
to Widget
TabPanel tabPanel = new TabPanel();
((Widget)tabPanel().getTabBar().getTab(tabsToDisable.iterator().next())).addStyleName("disabled");
精彩评论