0

I would like to add Cut / Copy / Paste items into my context menu I already have available in my JTree control.

I would like those actions just to initiate the default actions "cut", "copy", "paste" actions already implemented by the default JTree, with as little boilerplate code as possible. I have checked the actions are available in the ActionMap on the tree, I can do getActionMap().get("cut") to get the corresponding action, but I do not know how to proceed - there is a method called SwingUtilities.notifyAction (this is used when processing the default key bindings in the tree), but this method requires a few parameter values I do not have ready.

I expect the code could like a bit like this, only a different method needs to be used instead of notifyAction, or perhaps some sensible values could be contstructed for missing parameters:

        Action action = tree.getActionMap().get("cut");
        if (action != null) {
            SwingUtilities.notifyAction(action);
        }
Suma
  • 33,181
  • 16
  • 123
  • 191
  • See this answer by Vineet Kosaraju: http://stackoverflow.com/questions/20343716/my-custom-paste-from-clipboard-action – Manoj Vadehra Jan 14 '16 at 11:02
  • @ManojVadehra Thanks for effort, but this definitely seems too verbose for the purpose. I have no intention of implementing clipboard operations, they are already implemented in the container, I just want to trigger or call the existing implementation. – Suma Jan 14 '16 at 11:05
  • `new JMenuItem(action)` <- Basically use the `Action` with a `JMenuItem`. You might find you need to supply the text for the button though, but see where it takes you – MadProgrammer Jan 14 '16 at 11:22

1 Answers1

0

"I would like to add Cut / Copy / Paste items into my context menu I already have available in my JTree control."

Are you talking about a JPopupMenu? If yes, you can add JMenuItem for cut, copy and paste then just use addActionListener() on all of them.

Manoj Vadehra
  • 836
  • 4
  • 17