Dynamic Tab

This Code will Create Dynamic Two tabs namely Market and +. When user click on + tab, User will get New Tab. Such as
Tab1,New Tab(Displaying now Empty tab), +.

Here Tab1 is Static Tab. When Click on + button, code will generate dynamic tab. After Generating tab, I want to add or edit values in the Dynamic Tabs.

I have attached the screenshot of the Tabs.

package com.example.newdynamictab;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.MouseEvents.DoubleClickEvent;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Label;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TabSheet.SelectedTabChangeEvent;
import com.vaadin.ui.TabSheet.SelectedTabChangeListener;
import com.vaadin.ui.TabSheet.Tab;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings(“serial”)
@Theme(“newdynamictab”)
public class NewdynamictabUI extends UI {

@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = NewdynamictabUI.class)
public static class Servlet extends VaadinServlet {
}

@Override
protected void init(VaadinRequest request) {
    VerticalLayout verticalLayout = new VerticalLayout();
    final TabSheet tabSheet = new TabSheet();
    final Tab sampleTab = tabSheet.addTab(new Label(),"Market..");
    tabSheet.addTab(new TextField("Contents of the first tab"),"+");
    tabSheet.setImmediate(true);
    tabSheet.addSelectedTabChangeListener(new SelectedTabChangeListener() {

        @Override
    public void selectedTabChange(SelectedTabChangeEvent event) {
    TabSheet tabsheet = event.getTabSheet();
    Tab selectedTab = tabsheet.getTab(tabsheet.getSelectedTab());
    if (selectedTab != null) {
    if(selectedTab.getCaption().equals("+")){
           VerticalLayout newTabLayout = new VerticalLayout();
           Tab tab = tabSheet.addTab(newTabLayout, "", null);
           int newPosition = tabSheet.getTabPosition(tab);
           tabSheet.setTabPosition(tab,newPosition-1 );
           tabSheet.setTabPosition(selectedTab,newPosition );
           tabSheet.setSelectedTab(tab);
           
    } } } });    
    
    verticalLayout.addComponent(tabSheet);
    //verticalLayout.addComponent(button);
    setContent(verticalLayout);
    } // End Loop

}
14149.jpg

Nice example. To avoid swapping tabs after adding, you can add tabs using TabSheet.addTab(Component component, int position).

protected void init(VaadinRequest request) {
    final TabSheet tabSheet = new TabSheet();
    tabSheet.setImmediate(true);
    tabSheet.addTab(new Label("Some content"), "Market");

    final Tab addTab = tabSheet.addTab(new TextField("Contents of the first tab"), "+");

    tabSheet.addSelectedTabChangeListener(new SelectedTabChangeListener() {

        public void selectedTabChange(SelectedTabChangeEvent event) {
            TabSheet tabsheet = event.getTabSheet();

            Tab selectedTab = tabsheet.getTab(tabsheet.getSelectedTab());

            if (selectedTab != null) {
                if (selectedTab.getCaption().equals("+")) {
                    Tab tab = tabSheet.addTab(new VerticalLayout(), tabSheet.getTabPosition(addTab));
                    tab.setCaption("New Tab");
                    tabSheet.setSelectedTab(tab);
                }
            }
        }
    });

    setContent(tabSheet);
}