4

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()); 
    }                                   
StanislavL
  • 56,971
  • 9
  • 68
  • 98

2 Answers2

6

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!

MRSGT _GT
  • 246
  • 2
  • 11
  • No problem! but, please do read java swing and fully understand what the code does to extend your code!:) – MRSGT _GT Mar 15 '12 at 17:49
3
JEditorPane edit=... your instance;

Then use one of the

    edit.cut();
    edit.copy();
    edit.paste();
StanislavL
  • 56,971
  • 9
  • 68
  • 98