Here you can find the source of addRCMenuMouseListener(final JTextComponent text)
public static void addRCMenuMouseListener(final JTextComponent text)
//package com.java2s; import java.awt.event.*; import javax.swing.*; import javax.swing.text.*; public class Main { public static void addRCMenuMouseListener(final JTextComponent text) { text.addMouseListener(new MouseAdapter() { @Override/*from w ww. j a v a 2 s . com*/ public void mousePressed(MouseEvent e) { if (e.isMetaDown() && text.isEnabled()) { text.requestFocus(); showRCMenu(text, e); } } }); } private static void showRCMenu(JTextComponent text, MouseEvent e) { int selStart = text.getSelectionStart(); int selEnd = text.getSelectionEnd(); JPopupMenu rightClickMenu = new JPopupMenu(); JMenuItem copyMenuItem = new JMenuItem(text.getActionMap().get(DefaultEditorKit.copyAction)); JMenuItem cutMenuItem = new JMenuItem(text.getActionMap().get(DefaultEditorKit.cutAction)); JMenuItem pasteMenuItem = new JMenuItem(text.getActionMap().get(DefaultEditorKit.pasteAction)); JMenuItem selectAllMenuItem = new JMenuItem(text.getActionMap().get(DefaultEditorKit.selectAllAction)); copyMenuItem.setText("Copy"); cutMenuItem.setText("Cut"); pasteMenuItem.setText("Paste"); selectAllMenuItem.setText("Select All"); rightClickMenu.add(copyMenuItem); rightClickMenu.add(cutMenuItem); rightClickMenu.add(pasteMenuItem); rightClickMenu.addSeparator(); rightClickMenu.add(selectAllMenuItem); if (text.getText().isEmpty()) { copyMenuItem.setEnabled(false); selectAllMenuItem.setEnabled(false); cutMenuItem.setEnabled(false); } if (selStart == selEnd) { copyMenuItem.setEnabled(false); cutMenuItem.setEnabled(false); } if ((selStart + selEnd) == text.getText().length()) { selectAllMenuItem.setEnabled(false); } if (!text.isEditable()) { cutMenuItem.setEnabled(false); pasteMenuItem.setEnabled(false); } rightClickMenu.show(text, e.getX(), e.getY()); } }