0% found this document useful (0 votes)
36 views6 pages

Pop Up Menu

This document contains code for creating and handling popup menus in Java Swing. It defines a PopupMenu class that creates a JPopupMenu with menu items for common editing actions like cut, copy, paste. It also defines a PopupTest class that demonstrates using a popup menu to select different background colors for a frame. The popup menu is displayed using mouse listeners on the frame and contains radio button menu items in a button group to select one of three predefined colors.

Uploaded by

jineshraval20
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views6 pages

Pop Up Menu

This document contains code for creating and handling popup menus in Java Swing. It defines a PopupMenu class that creates a JPopupMenu with menu items for common editing actions like cut, copy, paste. It also defines a PopupTest class that demonstrates using a popup menu to select different background colors for a frame. The popup menu is displayed using mouse listeners on the frame and contains radio button menu items in a button group to select one of three predefined colors.

Uploaded by

jineshraval20
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

PopUpMenu.

java
import javax.swing.*; import java.awt.event.*;

public class PopUpMenu{ JPopupMenu Pmenu; JMenuItem menuItem; public static void main(String[] args) { PopUpMenu p = new PopUpMenu(); }

public PopUpMenu(){ JFrame frame = new JFrame("Creating a Popup Menu"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Pmenu = new JPopupMenu(); menuItem = new JMenuItem("Cut"); Pmenu.add(menuItem); menuItem = new JMenuItem("Copy"); Pmenu.add(menuItem); menuItem = new JMenuItem("Paste"); Pmenu.add(menuItem); menuItem = new JMenuItem("Delete"); Pmenu.add(menuItem); menuItem = new JMenuItem("Undo"); Pmenu.add(menuItem); menuItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){}

}); frame.addMouseListener(new MouseAdapter(){ public void mouseReleased(MouseEvent Me){ if(Me.isPopupTrigger()){ Pmenu.show(Me.getComponent(), Me.getX(), Me.getY()); } } }); frame.setSize(400,400); frame.setVisible(true); } }

PopupTest.java

import javax.swing.*; import java.awt.event.*; import java.awt.*;

public class PopupTest extends JFrame { private JRadioButtonMenuItem items[]; private Color colorValues[] = { Color.blue, Color.yellow, Color.red };

public PopupTest() { super( "Using JPopupMenus" );

final JPopupMenu popupMenu = new JPopupMenu(); ItemHandler handler = new ItemHandler(); String colors[] = { "Blue", "Yellow", "Red" }; ButtonGroup colorGroup = new ButtonGroup(); items = new JRadioButtonMenuItem[ 3 ];

// construct each menu item and add to popup menu; also // enable event handling for each menu item for ( int i = 0; i < items.length; i++ ) { items[ i ] = new JRadioButtonMenuItem( colors[ i ] ); popupMenu.add( items[ i ] );

colorGroup.add( items[ i ] ); items[ i ].addActionListener( handler ); }

getContentPane().setBackground( Color.white );

// define a MouseListener for the window that displays // a JPopupMenu when the popup trigger event occurs addMouseListener( new MouseAdapter() { public void mousePressed( MouseEvent e ) { checkForTriggerEvent( e ); }

public void mouseReleased( MouseEvent e ) { checkForTriggerEvent( e ); }

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

setSize( 300, 200 );

show(); }

public static void main( String args[] ) { PopupTest app = new PopupTest();

app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } }); }

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

} } }

You might also like