Question

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);
Was it helpful?

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

OTHER TIPS

Let Java String manipulation code do this for you.

for example, assuming an array of Actions:

private Action[] textActions = { new DefaultEditorKit.CutAction(),
     new DefaultEditorKit.CopyAction(), new DefaultEditorKit.PasteAction(), };
  1. Get the original name of the action via Action's getValue(...) method.
  2. Change the name to get rid of the -to-clipboard part by geting the subString that begins at the beginning of the String and ends at the "-": substring(0, value.indexOf("-"));
  3. Use subString again to start the String with a capital letter.
  4. Use the Action putValue(...) method to set the Action's name to our new value.

For example:

private Action[] textActions = { new DefaultEditorKit.CutAction(),
     new DefaultEditorKit.CopyAction(), new DefaultEditorKit.PasteAction(), };

public DisplayText(String title, String info) {
  JMenu menu = new JMenu("Edit");
  for (Action textAction : textActions) {
     String value = textAction.getValue(AbstractAction.NAME).toString();
     value = value.substring(0, value.indexOf("-"));
     value = value.substring(0, 1).toUpperCase() + value.substring(1);
     textAction.putValue(AbstractAction.NAME, value);
     menu.add(new JMenuItem(textAction));
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top