Java 更改选项卡名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9699974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:22:13  来源:igfitidea点击:

Change tab name

javaswingjtabbedpane

提问by Alberto acepsut

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.

我想通过添加 ActionPerformed 从弹出菜单中单击“重命名”项来更改选项卡名称,然后直接在选项卡上键入新名称。

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

我找到了这个 jTabbedPane.setTitleAt(count, "string here");

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

但这不是我想要的,这只是将字符串设置为参数。

Thanks

谢谢

回答by mKorbel

By adding JPopupMenuis possible to determine indexand getTitleAt()from JTabbedPane

通过添加JPopupMenu可以确定indexgetTitleAt()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);
            }
        });
    }
}