package example;
//-*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public final class MainPanel extends JPanel {
    private MainPanel() {
        super(new BorderLayout());

        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.setComponentPopupMenu(new TabbedPanePopupMenu());
        tabbedPane.addChangeListener(new TabChangeListener());
        tabbedPane.addTab("Title", new JLabel("Tab"));

        add(tabbedPane);
        setPreferredSize(new Dimension(320, 240));
    }
    public static void main(String... args) {
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                createAndShowGUI();
            }
        });
    }
    public static void createAndShowGUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException
               | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }
        JFrame frame = new JFrame("@title@");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class TabChangeListener implements ChangeListener {
    @Override public void stateChanged(ChangeEvent e) {
        JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
        if (tabbedPane.getTabCount() <= 0) {
            return;
        }
        int sindex = tabbedPane.getSelectedIndex();
        for (int i = 0; i < tabbedPane.getTabCount(); i++) {
            if (i == sindex && tabbedPane.getTitleAt(sindex).endsWith("1")) {
                tabbedPane.setForegroundAt(i, Color.GREEN);
            } else if (i == sindex) {
                Color sc = sindex % 2 == 0 ? Color.RED : Color.BLUE;
                tabbedPane.setForegroundAt(i, sc);
            } else {
                tabbedPane.setForegroundAt(i, Color.BLACK);
            }
        }
    }
}

class TabbedPanePopupMenu extends JPopupMenu {
    protected transient int count;
    private final JMenuItem closePage;
    private final JMenuItem closeAll;
    private final JMenuItem closeAllButActive;
    protected TabbedPanePopupMenu() {
        super();
        add("New tab").addActionListener(e -> {
            JTabbedPane tabbedPane = (JTabbedPane) getInvoker();
            tabbedPane.addTab("Title: " + count, new JLabel("Tab: " + count));
            tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1);
            count++;
            System.out.println(getComponent().getClass().getName());
        });
        addSeparator();
        closePage = add("Close");
        closePage.addActionListener(e -> {
            JTabbedPane tabbedPane = (JTabbedPane) getInvoker();
            tabbedPane.remove(tabbedPane.getSelectedIndex());
        });
        addSeparator();
        closeAll = add("Close all");
        closeAll.addActionListener(e -> {
            JTabbedPane tabbedPane = (JTabbedPane) getInvoker();
            tabbedPane.removeAll();
        });
        closeAllButActive = add("Close all bat active");
        closeAllButActive.addActionListener(e -> {
            JTabbedPane tabbedPane = (JTabbedPane) getInvoker();
            int tabidx = tabbedPane.getSelectedIndex();
            String title = tabbedPane.getTitleAt(tabidx);
            Component cmp = tabbedPane.getComponentAt(tabidx);
            tabbedPane.removeAll();
            tabbedPane.addTab(title, cmp);
        });
    }
    @Override public void show(Component c, int x, int y) {
        if (c instanceof JTabbedPane) {
            JTabbedPane tabbedPane = (JTabbedPane) c;
            //JDK 1.3: tabindex = tabbedPane.getUI().tabForCoordinate(tabbedPane, x, y);
            closePage.setEnabled(tabbedPane.indexAtLocation(x, y) >= 0);
            closeAll.setEnabled(tabbedPane.getTabCount() > 0);
            closeAllButActive.setEnabled(tabbedPane.getTabCount() > 0);
            super.show(c, x, y);
        }
    }
}

@@ -2740,77 +2740,8 @@
 ++;%0A
-            System.out.println(getComponent().getClass().getName());%0A
     

use JPopupMenu#add(String) instead of JPopupMenu#add(Action)