Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.BorderLayout;

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);
    }

}