Testing PopupFrame with JPopupMenus - Java Swing

Java examples for Swing:JPopupMenu

Description

Testing PopupFrame with JPopupMenus

Demo Code

import javax.swing.JFrame;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.ButtonGroup;

 class PopupFrame extends JFrame 
{
   private final JRadioButtonMenuItem[] items; // holds items for colors
   private final Color[] colorValues = 
      {Color.BLUE, Color.YELLOW, Color.RED}; // colors to be used
   private final JPopupMenu popupMenu; // allows user to select color

   // no-argument constructor sets up GUI
   public PopupFrame()
   {//  w w  w  .  j a  v a 2 s .  c o m
      super("Using JPopupMenus");

      ItemHandler handler = new ItemHandler(); // handler for menu items
      String[] colors = {"Blue", "Yellow", "Red"};

      ButtonGroup colorGroup = new ButtonGroup(); // manages color items
      popupMenu = new JPopupMenu(); // create pop-up menu
      items = new JRadioButtonMenuItem[colors.length];

      // construct menu item, add to pop-up menu, enable event handling
      for (int count = 0; count < items.length; count++) 
      {
         items[count] = new JRadioButtonMenuItem(colors[count]);
         popupMenu.add(items[count]); // add item to pop-up menu
         colorGroup.add(items[count]); // add item to button group
         items[count].addActionListener(handler); // add handler
      }

      setBackground(Color.WHITE); 

      addMouseListener(
         new MouseAdapter()
         {  
            @Override
            public void mousePressed(MouseEvent event)
            { 
               checkForTriggerEvent(event); 
            } 

            @Override
            public void mouseReleased(MouseEvent event)
            { 
               checkForTriggerEvent(event);
            }

            private void checkForTriggerEvent(MouseEvent event)
            {
               if (event.isPopupTrigger()) 
                  popupMenu.show(
                     event.getComponent(), event.getX(), event.getY());  
            }
         } 
      ); 
   } 

   private class ItemHandler implements ActionListener 
   {
      @Override
      public void actionPerformed(ActionEvent event)
      {
         // determine which menu item was selected
         for (int i = 0; i < items.length; i++)
         {
            if (event.getSource() == items[i]) 
            {
               getContentPane().setBackground(colorValues[i]);
               return;
            } 
         } 
      } 
   }
}

public class Main 
{
   public static void main(String[] args)
   { 
      PopupFrame popupFrame = new PopupFrame();
      popupFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      popupFrame.setSize(300, 200); 
      popupFrame.setVisible(true);
   } 
}

Related Tutorials