We would like to know how to add tab in JTabbedPane like google chrome.
import java.awt.BorderLayout; //ww w .j av a2 s .c om import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class Main { public static void main(String[] args) { JTabbedPane tab = new JTabbedPane(); tab.addTab("A", new JPanel()); tab.addTab("+", new JPanel()); tab.getModel().addChangeListener(new ChangeListener() { private int lastSelected; private boolean ignore = false; @Override public void stateChanged(ChangeEvent e) { if (!ignore) { ignore = true; try { int selected = tab.getSelectedIndex(); String title = tab.getTitleAt(selected); if ("+".equals(title)) { JPanel pane = new JPanel(); tab.insertTab("Tab" + (tab.getTabCount() - 1), null, pane, null, lastSelected + 1); tab.setSelectedComponent(pane); } else { lastSelected = selected; } } finally { ignore = false; } } } }); final JButton btn = new JButton("Add"); btn.addActionListener(e->System.out.println(tab.getTabCount())); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(tab); frame.add(btn, BorderLayout.SOUTH); frame.pack(); frame.setVisible(true); } }