简体   繁体   English

在菜单中动态添加menuitem而不关闭菜单

[英]Dynamically add menuitem in menu without closing the menu

I want to add JMenuItem without closing JMenu . 我想在不关闭JMenu情况下添加JMenuItem A menu contains a JTextField , When I press enter in text field, it added a menu Item. 菜单包含一个JTextField ,当我在文本字段中输入时,它添加了一个菜单项。 My problem is the size of added menu item is too small. 我的问题是添加菜单项的大小太小。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.JTextField;

public class NewJFrame extends javax.swing.JFrame {

public NewJFrame() {
    initComponents();
    this.jMenu1.addSeparator();
    final JTextField jTextField = new JTextField("Menu 1",20);
    this.jMenu1.add(jTextField);   

    jTextField.addActionListener(new ActionListener( ){
        @Override
        public void actionPerformed(ActionEvent e) { 
           final JMenuItem jMenuItem = new JMenuItem(jTextField.getText());
            jMenu1.add(jMenuItem);
             jMenuItem.repaint();
             jMenuItem.revalidate();

        }    
    });

}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jMenu1.setText("File");
    jMenuBar1.add(jMenu1);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 279, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>//GEN-END:initComponents

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}


// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
// End of variables declaration//GEN-END:variables
}

When I am pressing enter, menu add a menu item, but its width is too small. 当我按Enter键时,菜单添加一个菜单项,但其宽度太小。

How can I fix it? 我该如何解决?

You have to update the popupMenu of the jMenu1, calling: 你必须更新jMenu1的popupMenu,调用:

jMenu1.getPopupMenu().pack();

instead of 代替

jMenuItem.repaint();
jMenuItem.revalidate();

Instead of repainting it, close the menu and reopen it. 不要重新绘制它,关闭菜单并重新打开它。 Just tried it and it works great, it's as quick as repainting on the spot. 刚刚尝试过,效果很好,就像在现场重新粉刷一样快。

jMenu1.add(jMenuItem);
//jMenuItem.repaint();
//jMenuItem.revalidate();
MenuElement[] selectionPath = MenuSelectionManager.defaultManager().getSelectedPath();
MenuSelectionManager.defaultManager().clearSelectedPath();
MenuSelectionManager.defaultManager().setSelectedPath(selectionPath);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM