Right click to copy and paste in Java

I use Netbeans to develop an application that will be used on Windows. I noticed that I can not right-click to copy or paste. How can I enable this? (I use basic Swing controls such as JText and JTextArea.)

+4
source share
5 answers

Why doesn't right click work in a Java application?

I will not create a new copy, cut, paste, undo and select all actions, because they already exist inside the ActionMap of each component. I would just do:

Action copyAction = textField.getActionMap().get("copy"); Action cutAction = textField.getActionMap().get("cut"); Action pasteAction = textField.getActionMap().get("paste"); Action undoAction = textField.getActionMap().get("undo"); Action selectAllAction = textField.getActionMap().get("selectAll"); popup.add (undoAction); popup.addSeparator(); popup.add (cutAction); popup.add (copyAction); popup.add (pasteAction); popup.addSeparator(); popup.add (selectAllAction); return popup; 

This way you will not create more code that is already written. Other than that, I will follow this example.

+12
source

For your comment

Does it interact with the operating system? I want to copy something from my Java application and paste it into Notepad.

Why bother the user manually copy-paste, why not write the contents of the text area to a text file?

+1
source
 final int colx; final int rowy; final String val1; colx = CFDTable.getSelectedColumn(); rowy = CFDTable.getSelectedRow(); val1 = (String) CFDTable.getValueAt(rowy, colx); JPopupMenu jPopupMenu = new javax.swing.JPopupMenu(); jPopupMenu.setName("jPopupMenu"); CFDTable.setComponentPopupMenu(jPopupMenu); JMenuItem jMenuItem = new javax.swing.JMenuItem(); jMenuItem.setText("Copy"); // NOI18N jMenuItem.setName("jMenuItem"); // NOI18N jPopupMenu.add(jMenuItem); jMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { StringSelection entry = new StringSelection(val1); Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); clipboard.setContents(entry, entry); } }); 
+1
source

Full working model for any JTextComponent, including non-functional actions and turning on / off menu items subject to text selection.

  private JMenuItem composeMenuItemFromActionOfComponent(String actionTag, JComponent component, String text, String idTag){ Action action = component.getActionMap().get(actionTag); JMenuItem menuItem = new JMenuItem(action); menuItem.setText(text); menuItem.putClientProperty("id",idTag); if(action == null) menuItem.setEnabled(false); return menuItem; } private void addTextMenuItems(JTextComponent textField){ final JPopupMenu popup = new JPopupMenu(); if(textField.isEditable()) { popup.add(composeMenuItemFromActionOfComponent("undo", textField, "Undo", "undo")); popup.addSeparator(); } popup.add (composeMenuItemFromActionOfComponent(DefaultEditorKit.copyAction,textField,"Copy", "copy")); if(textField.isEditable()) { popup.add(composeMenuItemFromActionOfComponent(DefaultEditorKit.cutAction, textField, "Cut", "cut")); popup.add(composeMenuItemFromActionOfComponent(DefaultEditorKit.pasteAction, textField, "Paste", "paste")); popup.addSeparator(); } popup.add (composeMenuItemFromActionOfComponent(DefaultEditorKit.selectAllAction,textField,"Select All", "select_all")); textField.addMouseListener(new MouseListener() { @Override public void mouseClicked(MouseEvent e) {} @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) { if(e.isPopupTrigger()) { boolean isTextSelected = textField.getSelectedText() != null; for(MenuElement element : popup.getSubElements()){ JMenuItem menuItem = ((JMenuItem) element); if(menuItem.getAction() != null && menuItem.getAction().isEnabled()) { if (isTextSelected){ menuItem.setEnabled(true); }else if (!(menuItem.getClientProperty("id").equals("select_all") || menuItem.getClientProperty("id").equals("undo"))) menuItem.setEnabled(false); } if(menuItem.getClientProperty("id").equals("paste")){ boolean isPastAvailable = false; for(DataFlavor flavor : Toolkit.getDefaultToolkit().getSystemClipboard().getAvailableDataFlavors()){ if(flavor.getRepresentationClass() == String.class){ isPastAvailable = true; break; } } menuItem.setEnabled(isPastAvailable); } } e.getComponent().requestFocus(); popup.show(e.getComponent(), e.getX(), e.getY()); } } @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseExited(MouseEvent e) {} }); } 
0
source

Static class to instantly add a regular popup menu to a text box.

Just call JTextFieldRegularPopupMenu.addTo (jTextFieldObj);

 import javax.swing.*; import java.awt.event.ActionEvent; import javax.swing.JPopupMenu; import javax.swing.undo.*; public class JTextFieldRegularPopupMenu { public static void addTo(JTextField txtField) { JPopupMenu popup = new JPopupMenu(); UndoManager undoManager = new UndoManager(); txtField.getDocument().addUndoableEditListener(undoManager); Action undoAction = new AbstractAction("Undo") { @Override public void actionPerformed(ActionEvent ae) { if (undoManager.canUndo()) { undoManager.undo(); } else { JOptionPane.showMessageDialog(null, "Undoable: " + undoManager.canUndo() , "Undo Status", JOptionPane.INFORMATION_MESSAGE); } } }; Action copyAction = new AbstractAction("Copy") { @Override public void actionPerformed(ActionEvent ae) { txtField.copy(); } }; Action cutAction = new AbstractAction("Cut") { @Override public void actionPerformed(ActionEvent ae) { txtField.cut(); } }; Action pasteAction = new AbstractAction("Paste") { @Override public void actionPerformed(ActionEvent ae) { txtField.paste(); } }; Action selectAllAction = new AbstractAction("Select All") { @Override public void actionPerformed(ActionEvent ae) { txtField.selectAll(); } }; popup.add (undoAction); popup.addSeparator(); popup.add (cutAction); popup.add (copyAction); popup.add (pasteAction); popup.addSeparator(); popup.add (selectAllAction); txtField.setComponentPopupMenu(popup); } } 
0
source

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


All Articles