Popup Menu in Java Swing

A PopupMenu is a menu(which contains more than one menus) which is not visible until the user make a specific mouse action(right click).To create a popup menu we use the class JPopupMenu after that we can add menu items JMenuItem to a Popup Menu.

 

PopupMenu response with a mouse event means we must register a mouse listener on each and every component.JPopupMenu() is used to create a popup menu.

import java.awt.datatransfer.Clipboard;

import java.awt.datatransfer.DataFlavor;

import java.awt.datatransfer.StringSelection;

import java.awt.datatransfer.Transferable;

import java.awt.event.MouseEvent;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class RightClickPopupDemo extends JFrame

{

                JTextArea jtextarea;

                Container contentarea;

                JPopupMenu popup;

                Clipboard clipboard;

               

               

                public RightClickPopupDemo()

{

                                super("Right Click Popup Demo");

                               

                                clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

                               

                                contentarea = getContentPane();

                               

                                jtextarea = new JTextArea(10,10);

                               

                                popup = new JPopupMenu();

                               

                                JMenuItem popupMenuItemCut = new JMenuItem("Cut");

                                JMenuItem popupMenuItemCopy = new JMenuItem("Copy");

                                JMenuItem popupMenuItemPaste = new JMenuItem("Paste");

                                JMenuItem popupMenuItemExit = new JMenuItem("Exit");

                               

                                ActionListener actionListener = new MenuItemListenerr();

                               

                                popupMenuItemCut.addActionListener(actionListener);

                                popupMenuItemCopy.addActionListener(actionListener);

                                popupMenuItemPaste.addActionListener(actionListener);

                                popupMenuItemExit.addActionListener(actionListener);

                               

                                popup.add(popupMenuItemCut);

                                popup.add(popupMenuItemCopy);

                                popup.add(popupMenuItemPaste);

                                popup.add(new JSeparator());

                                popup.add(popupMenuItemExit);

                               

                               

                                jtextarea.addMouseListener(new PopupListenerr());

                               

                                int vsb = JScrollPane.VERTICAL_SCROLLBAR_ALWAYS;

                                int hsb = JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS;

                               

                                contentarea.add(new JScrollPane(jtextarea, vsb,hsb));

                               

                               

                                setSize(300,300);

                }

               

                class PopupListenerr extends MouseAdapter

                       {

                   public void mousePressed(MouseEvent mouseevnt)

                         {

                       showPopup(mouseevnt);

                   }

                   public void mouseReleased(MouseEvent mouseevt)

                         {

                       showPopup(mouseevt);

                     }

                   private void showPopup(MouseEvent mouseevnt)

                       {

                       if (mouseevnt.isPopupTrigger())

                               {

                           popup.show(mouseevnt.getComponent(),

                                       mouseevnt.getX(),

                                         mouseevnt.getY());

                       }

                   }

                }

                class MenuItemListenerr implements ActionListener

                       {

                               

                       public void actionPerformed(ActionEvent actionevent) //user click a menu option

                         {

                         String cmd = actionevent.getActionCommand();

                       char choice = cmd.charAt(1);

                       String text = "";

                       switch(choice)

                             {

                           case 'u':

                               {

                                text=jtextarea.getSelectedText();

                                jtextarea.replaceRange("",jtextarea.getSelectionStart(),jtextarea.getSelectionEnd());                                                                 

                                StringSelection stringselected = new StringSelection(text);

                                clipboard.setContents(stringselected, null);

                                break;

                           }

                         case 'o':

                               {

                                text=jtextarea.getSelectedText();

                                StringSelection stringselected = new StringSelection(text);

                                clipboard.setContents(stringselected, null);

                                break;

                         }

                     case 'a':

                           {

                                                                               

                                   Transferable transdata = clipboard.getContents(null);

                                try

                                       {

                                   String pasteString = (String)transdata.getTransferData(DataFlavor.stringFlavor);

                                   jtextarea.replaceRange(pasteString,jtextarea.getSelectionStart(),jtextarea.getSelectionEnd());

                                }

                                         catch (Exception e)

                                         {                                                                                    

                                System.out.println("Exception "+e);

                                }

                                break;

                 }

                case 'x':

                         {                                                                    

                         System.exit(0);

                         break;

                   }

                }

             }

         }

               

                public static void main(String[] args)

                       {

                       new RightClickPopupDemo().setVisible(true);

                   }

   }

                PopupMenu in java swing

Explanation…

Void show() is used to display the popup menu at the specified position(x,y).

addSeparator() is used to adds a separator to the current end of the popup menu.

JMenuItem() is used to creates an ordinary menu.