Dynamically add menuitem in menu without closing the menu
Asked Answered
C

2

5

I want to add JMenuItem without closing JMenu. A menu contains a JTextField, When I press enter in text field, it added a menu Item. 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.

How can I fix it?

Cohette answered 3/9, 2013 at 6:40 Comment(1)
Why 'without closing menu'? A quick close and open is not visible to the naked eye, and fixes the problem.Finable
C
2

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);
Cartwell answered 3/9, 2013 at 6:54 Comment(0)
M
5

You have to update the popupMenu of the jMenu1, calling:

jMenu1.getPopupMenu().pack();

instead of

jMenuItem.repaint();
jMenuItem.revalidate();
Marva answered 3/9, 2013 at 7:12 Comment(0)
C
2

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);
Cartwell answered 3/9, 2013 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.