Change tab name

14,609

By adding JPopupMenu is possible to determine index and getTitleAt() from JTabbedPane

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class TabPopupDemo extends JFrame {

    private static final long serialVersionUID = 1L;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JMenuItem jMenuItem1;
    private JPopupMenu jPopupMenu1;
    private JTabbedPane jTabbedPane1;

    public TabPopupDemo() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);
        jPopupMenu1 = new JPopupMenu();
        jMenuItem1 = new JMenuItem("jMenuItem1");
        jTabbedPane1 = new JTabbedPane();
        jLabel1 = new JLabel("jLabel1");
        jLabel2 = new JLabel("jLabel2");
        jPopupMenu1.add(jMenuItem1);
        jTabbedPane1.addTab(null, jLabel1);
        jTabbedPane1.addTab(null, jLabel2);
        getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
        int tabCount = jTabbedPane1.getTabCount();
        for (int i = 0; i < tabCount; i++) {
            JLabel jLabel = new JLabel("Testing the tab" + (i + 1));
            jTabbedPane1.setTabComponentAt(i, jLabel);
            jLabel.setName(String.valueOf(i));
            jLabel.setComponentPopupMenu(jPopupMenu1);
        }
        jPopupMenu1.addPopupMenuListener(new PopupMenuListener() {

            @Override
            public void popupMenuCanceled(final PopupMenuEvent evt) {
            }

            @Override
            public void popupMenuWillBecomeInvisible(final PopupMenuEvent evt) {
            }

            @Override
            public void popupMenuWillBecomeVisible(final PopupMenuEvent evt) {
                JPopupMenu source = (JPopupMenu) evt.getSource();
                JLabel invoker = (JLabel) source.getInvoker();
                JLabel component = (JLabel) jTabbedPane1.getComponentAt(Integer.parseInt(invoker.getName()));
                jMenuItem1.setText(invoker.getText() + ":  " + component.getText());
            }
        });
    }

    public static void main(final String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TabPopupDemo().setVisible(true);
            }
        });
    }
}
Share:
14,609
Alberto acepsut
Author by

Alberto acepsut

Cycles researcher for financial markets

Updated on June 26, 2022

Comments

  • Alberto acepsut
    Alberto acepsut almost 2 years

    enter image description here

    I would like to change tab name by clicking on "Rename" item from a popup menu by adding an ActionPerformed, and type directly on tab the new name.

    I have found this jTabbedPane.setTitleAt(count, "string here");

    but it is not what I want, this just set the string passed as argument.

    Thanks

  • kleopatra
    kleopatra about 12 years
    narrative? The typical tab isn't long enough for such a thingy :-)
  • mKorbel
    mKorbel about 12 years
    @kleopatra you are right, thank you, true is that .... in most cases I using nomenclatures that doesn't exists on this word