How to enable copy/cut/paste jMenuItem

donthedestroyer picture donthedestroyer · Mar 15, 2012 · Viewed 8.3k times · Source

I am making text editor in netbeans and have added jMenuItems called Copy,Cut & Paste in the Edit menu.

How do I enable these buttons to perform these functions after actionPerformed()

Here is my attempt:

    private void CopyActionPerformed(java.awt.event.ActionEvent evt) {                                     

       JMenuItem Copy = new JMenuItem(new DefaultEditorKit.CopyAction()); 
    }                                    

    private void PasteActionPerformed(java.awt.event.ActionEvent evt) {                                      
     JMenuItem Paste = new JMenuItem(new DefaultEditorKit.PasteAction()); 
    }                                     

    private void CutActionPerformed(java.awt.event.ActionEvent evt) {                                    
       JMenuItem Cut = new JMenuItem(new DefaultEditorKit.CutAction()); 
    }                                   

Answer

MRSGT _GT picture MRSGT _GT · Mar 15, 2012

simple editor example with cut ,copy ,paste:

      public class SimpleEditor extends JFrame {

      public static void main(String[] args) {
      JFrame window = new SimpleEditor();
      window.setVisible(true);
      }
      private JEditorPane editPane;   

      public SimpleEditor() {
      editPane = new JEditorPane("text/rtf","");
      JScrollPane scroller = new JScrollPane(editPane);
      setContentPane(scroller);
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      JMenuBar bar = new JMenuBar();
      setJMenuBar(bar);
      setSize(600,500);

      JMenu editMenu = new JMenu("Edit");

      Action cutAction = new DefaultEditorKit.CutAction();
      cutAction.putValue(Action.NAME, "Cut");
      editMenu.add(cutAction);

      Action copyAction = new DefaultEditorKit.CopyAction();
      copyAction.putValue(Action.NAME, "Copy");
      editMenu.add(copyAction);

      Action pasteAction = new DefaultEditorKit.PasteAction();
      pasteAction.putValue(Action.NAME, "Paste");
      editMenu.add(pasteAction);

      bar.add(editMenu);
   }

}

Hope this helps!