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
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
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
.
TabLayoutPanel
type, not TabBar
. –
Goff TabLayoutPanel
contains a TabBar
! That is the part at the top of the TabLayoutPanel
that holds the tabs. You can access the TabBar
of a TabLayoutPanel
by using myTabLayoutPanel.getTabBar()
(see my answer). I've updated my answer to make this more clear. –
Ghetto getTabBar
method on the TabLayoutPanel
type. Perhaps you mean TabPanel
? –
Goff TabPanel
. I've corrected my code. –
Ghetto You can access tab style by casting class Tab
to Widget
TabPanel tabPanel = new TabPanel();
((Widget)tabPanel().getTabBar().getTab(tabsToDisable.iterator().next())).addStyleName("disabled");
© 2022 - 2024 — McMap. All rights reserved.