Java Swing Menu showContextMenu()

Here you can find the source of showContextMenu()

Description

show Context Menu

License

Open Source License

Declaration

public static JPopupMenu showContextMenu() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.swing.Action;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.text.StyledEditorKit;

public class Main {
    public static JPopupMenu showContextMenu() {
        JPopupMenu menu = new JPopupMenu();

        Action cutAction = new StyledEditorKit.CutAction();
        String cutActionCommand = (String) cutAction.getValue(Action.ACTION_COMMAND_KEY);
        JMenuItem cutItem = new JMenuItem();
        cutItem.setActionCommand(cutActionCommand);
        cutItem.addActionListener(cutAction);
        cutItem.setText("Cut");
        menu.add(cutItem);/* w w  w . j  a  v a  2  s.  c o  m*/

        Action copyAction = new StyledEditorKit.CopyAction();
        String copyActionCommand = (String) copyAction.getValue(Action.ACTION_COMMAND_KEY);
        JMenuItem copyItem = new JMenuItem();
        copyItem.setActionCommand(copyActionCommand);
        copyItem.addActionListener(copyAction);
        copyItem.setText("Copy");
        menu.add(copyItem);

        Action pasteAction = new StyledEditorKit.PasteAction();
        String pasteActionCommand = (String) pasteAction.getValue(Action.ACTION_COMMAND_KEY);
        JMenuItem pasteItem = new JMenuItem();
        pasteItem.setActionCommand(pasteActionCommand);
        pasteItem.addActionListener(pasteAction);
        pasteItem.setText("Paste");
        menu.add(pasteItem);

        return menu;
    }
}

Related

  1. setFontsToMenu(MenuElement menuElement, Font font)
  2. setMenuFont(Object item, Font theFont)
  3. setMenuText(AbstractButton item, String text, boolean useMnemonic)
  4. setMenuUp(boolean s, JComponent c)
  5. setViewportMenu(JComponent c)
  6. showRCMenu(JTextComponent text, MouseEvent e)
  7. toMenu(List actions)