TabSheet - Close selected tab and select tab selected prior to last tab cha

I am trying to modify the behavior of the tab closing event so that if you close the currently selected tab, the most recently selected tab will be selected instead of the first tab in the list. I thought that I found an example that would capture this on the examples (
http://dev.vaadin.com/svn/doc/book-examples/trunk/src/com/vaadin/book/examples/layout/TabSheetExample.java
), but it doesn’t seem to work for me. It returns the same tab that is currently selected.

Here is my tab change and close handler code:


		stack.addListener(new TabSheet.SelectedTabChangeListener()
		{	
			private static final long serialVersionUID = 8175413689164828595L;
			
			@Override
			public void selectedTabChange(SelectedTabChangeEvent event)
			{				
				if (event.getTabSheet().getSelectedTab() instanceof IGadget)
				{
					try
					{
						((IGadget) event.getTabSheet().getSelectedTab()).onActivation();
					}catch (Throwable t) {}
				}
				
				newSelectedTab = event.getTabSheet().getSelectedTab();
			}
		});
		
		
		
		stack.setCloseHandler(new CloseHandler() 
		{
			
			@Override
			public void onTabClose(TabSheet tabsheet, Component tabContent) {
				if (newSelectedTab == tabContent)
				{
					stack.setSelectedTab(previousSelectedTab);
				}
				
				stack.removeComponent(tabContent);
			}
		});

previouslySelectedTab is currently undefined. That is the component that I want to hold the value of the last active tab prior to the one that is being closed.

Thoughts on how I can accomplish this? I’m sure that this is a common request, but I can’t find any examples of how to get it done.

Thanks!

FYI - Vaadin 6.8.10

This is what I’m trying, based on the example in the link above:


		stack.addListener(new TabSheet.SelectedTabChangeListener()
		{	
			private static final long serialVersionUID = 8175413689164828595L;
			
			Component selected = stack.getSelectedTab();
			
			@SuppressWarnings("synthetic-access")
			@Override
			public void selectedTabChange(SelectedTabChangeEvent event)
			{				
				if (event.getTabSheet().getSelectedTab() instanceof IGadget)
				{
					try
					{
						((IGadget) event.getTabSheet().getSelectedTab()).onActivation();
					}catch (Throwable t) {}
				}
				
				previousSelectedTab = selected;
				newSelectedTab = event.getTabSheet().getSelectedTab();
			}
		});

It doesn’t appear that the selected variable is ever getting created. It is null in my debug session when I click to create a new tab.

UPDATE: The selected variable is being created, but it is always null. I can’t seem to get it to capture the originating tab.

I still haven’t gotten anywhere on this. My code appears to follow the format of the Vaadin provided example, but when my code tries to get the currently selected tab it come back as null. I have ensured that i clicked between tabs several times before closing one to ensure that a tab is in fact selected before I select and close the other tab.

Any advice is greatly appreciated!

Still spinning my wheels on this one. I’ve stepped away for a while and come back to it, but I’m still not having any luck. Thoughts?

I can’t say I’m a total Java expert, but to me it seems that the selected variable is created and populated only once when the listener is created, and most likely that is before you have added any tabs to the TabSheet yet, so it’s always null.

And I think it should be more like this:

previousSelectedTab = selected;
selected = event.getTabSheet().getSelectedTab();

You shouldn’t in my opinion need a variable called ‘newSelectedTab’

Thanks, Jouni. That did it. I knew it was something simple, but I just couldn’t get it right. I appreciate your help!