Search code examples
javaswingactionjmenujmenuitem

How to rename submenu items?


I have a submenu populated with some actions, however the name that appears on them is not desirable. Instead of "Copy" and "Paste", I get the less desirable: copy-to-clipboard, paste-to-clipboard. I need to change that.

      //Submenu           
       SubMenu = new JMenu("Paste");
           menuOptions.add(SubMenu); 

           Action textActionCopy = new DefaultEditorKit.CopyAction();
           Action textActionPaste = new DefaultEditorKit.PasteAction();

           //Copy
           SubMenu.add(textActionCopy);

           //Paste
           SubMenu.add(textActionPaste);

Solution

  • How about create an MenuItem and add the action then add to menu..

    Sample:

           //Submenu           
           subMenu = new JMenu("Paste");
           menuOptions.add(SubMenu); 
    
           JMenuItem cut = new JMenuItem(new DefaultEditorKit.CutAction());
           JMenuItem copy = new JMenuItem(new DefaultEditorKit.CopyAction());
           JMenuItem paste = new JMenuItem(new DefaultEditorKit.PasteAction());
           subMenu .add(cut);
    
           paste.setText("Paste");
           cut.setText("Cut");
           copy.setText("Copy");
    
           subMenu .add(copy);
           subMenu .add(paste);
    

    Also follow the Java convention for variable names