Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Exemple de création et d’utilisation d’un JPopupMenu

 list, list, set, java, swing, awt, JFrame, Menu
{filelink=3547}

 
   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;
 
  public class ExempleJPopupMenu extends JFrame
  {
     private JRadioButtonMenuItem items[];
     private final Color colorValues[] =
        { Color.BLUE, Color.YELLOW, Color.RED };
     private JPopupMenu popupMenu;
 
     public ExempleJPopupMenu()
     {
        super( "Exemple JPopupMenus" );
 
        ItemHandler handler = new ItemHandler();
        String colors[] = { "Bleu", "Jaune", "Rouge" }; 
 
        ButtonGroup colorGroup = new ButtonGroup();
        popupMenu = new JPopupMenu();
        items = new JRadioButtonMenuItem[ 3 ]; 
 
        for ( int count = 0; count < items.length; count++ )
        {
           items[ count ] = new JRadioButtonMenuItem( colors[ count ] );
           popupMenu.add( items[ count ] );
           colorGroup.add( items[ count ] );
           items[ count ].addActionListener( handler );
        } 
 
        setBackground( Color.WHITE ); 
 
        addMouseListener(
 
           new MouseAdapter()
           {
 
              public void mousePressed( MouseEvent event )
              {
                 checkForTriggerEvent( event );
              } 
 
              public void mouseReleased( MouseEvent event )
              {
                 checkForTriggerEvent( event );
              } 
 
              private void checkForTriggerEvent( MouseEvent event )
              {
                 if ( event.isPopupTrigger() )
                    popupMenu.show(
                       event.getComponent(), event.getX(), event.getY() );
              }
           }
        );
        setSize(200,250);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     } 
 
     private class ItemHandler implements ActionListener
     {
 
       public void actionPerformed( ActionEvent event )
        {
 
           for ( int i = 0 ; i < items.length; i++ )
           {
              if ( event.getSource() == items[ i ] )
              {
                 getContentPane().setBackground( colorValues[ i ] );
                 return;
              }
           }
        }
     }
     	public static void main (String[] args)
     		{
     			new ExempleJPopupMenu().setVisible(true);
            }
  }

A Voir sur le même Sujet:

  • mail colorgroup.com loc:FR
  • EXEMPLES JPOPUPMENU
  • ajouter un separateur dans un popupmenu java
  • naviguer dossier
  • barre de progression de page jFrame en java
  • utiliser jpopupmenu
  • java afficher jpopupmenu
  • ajouter un jpopmenu à jpopmenu
  • jpopupmenu
  • jpopupmenu java 7 sample