Java Swing Menu addRCMenuMouseListener(final JTextComponent text)

Here you can find the source of addRCMenuMouseListener(final JTextComponent text)

Description

add RC Menu Mouse Listener

License

Open Source License

Declaration

public static void addRCMenuMouseListener(final JTextComponent text) 

Method Source Code


//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());
    }
}

Related

  1. addBooleanActionTo(Container menuOrToolBar, Action action)
  2. addHoverEffect4MenuAbout(final Component component, final Color overbgcolor, final Color overfgcolor, final Color outbgcolor, final Color outfgcolor)
  3. appendMenuSubElements(MenuElement element, StringBuilder builder, String indent)
  4. applyContextMenuFontRecurse(MenuElement item, Font font)
  5. buildManualsMenu(File appDir)
  6. constructViewMenu(ActionListener act, boolean showUMLOption, boolean showShortenedSourceOption, boolean showJavadocsOption, boolean showSourceOption)