How to enable copy / cut / paste jMenuItem

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

How to 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()); } 
+4
source share
2 answers

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!

+6
source
 JEditorPane edit=... your instance; 

Then use one of

  edit.cut(); edit.copy(); edit.paste(); 
+3
source

Source: https://habr.com/ru/post/1401594/


All Articles