Popup menu represents a menu which can be dynamically popped up at a specified position within a component.
Class Declaration
Following is the declaration for javax.swing.JPopupMenu class −
public class JPopupMenu extends JComponent implements Accessible, MenuElement
Class Constructors
Sr.No. | Constructor & Description |
---|---|
1 | JPopupMenu() Constructs a JPopupMenu without an "invoker". |
2 | JPopupMenu(String label) Constructs a JPopupMenu with the specified title. |
Class Methods
Methods Inherited
This class inherits methods from the following classes −
- javax.swing.JComponent
- java.awt.Container
- java.awt.Component
- java.lang.Object
import javax.swing.*;
import java.awt.event.*;
class PopupMenuExample
{
PopupMenuExample(){
final JFrame f= new JFrame("PopupMenu Example");
final JPopupMenu popupmenu = new JPopupMenu("Edit");
JMenuItem cut = new JMenuItem("Cut");
JMenuItem copy = new JMenuItem("Copy");
JMenuItem paste = new JMenuItem("Paste");
popupmenu.add(cut); popupmenu.add(copy); popupmenu.add(paste);
f.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
popupmenu.show(f , e.getX(), e.getY());
}
});
f.add(popupmenu);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new PopupMenuExample();
}
}
OUTPUT
ANOTHER JPOPUPMENU EXAMPLE
import java.awt.Container;
import javax.swing.*;
import java.awt.event.*;
public class PopupMenuExample extends JFrame
{
JPopupMenu popup;
public PopupMenuExample()
{
Container c = getContentPane( ) ;
popup = new JPopupMenu();
// add menu items to popup
popup.add(new JMenuItem("undo"));
popup.add(new JMenuItem("redo"));
popup.addSeparator();
popup.add(new JMenuItem("cut"));
popup.add(new JMenuItem("copy"));
popup.add(new JMenuItem("paste"));
c.addMouseListener(new MouseAdapter()
{
public void mouseReleased(MouseEvent e)
{
showPopup(e); // showPopup() is our own user-defined method
} } ) ;
setTitle("PopupMenu by sjavaspot");
setSize(500, 300);
setVisible(true);
}
void showPopup( MouseEvent e )
{
if(e.isPopupTrigger())
popup.show(e.getComponent(), e.getX( ), e.getY());
}
public static void main(String args[])
{
new PopupMenuExample();
}
}
OUTPUT:
ANOTHER JPOPUPMENU CODE AND OUTPUT
import java.awt.*; import java.awt.event.*; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; public class SwingJpopupMenu{ private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingJpopupMenu(){ prepareGUI(); } public static void main(String[] args){ SwingJpopupMenu swingMenuDemo = new SwingJpopupMenu(); swingMenuDemo.showPopupMenuDemo(); } private void prepareGUI(){ mainFrame = new JFrame("Java SWING Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); headerLabel = new JLabel("",JLabel.CENTER ); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showPopupMenuDemo(){ final JPopupMenu editMenu = new JPopupMenu("Edit"); JMenuItem cutMenuItem = new JMenuItem("Cut"); cutMenuItem.setActionCommand("Cut"); JMenuItem copyMenuItem = new JMenuItem("Copy"); copyMenuItem.setActionCommand("Copy"); JMenuItem pasteMenuItem = new JMenuItem("Paste"); pasteMenuItem.setActionCommand("Paste"); MenuItemListener menuItemListener = new MenuItemListener(); cutMenuItem.addActionListener(menuItemListener); copyMenuItem.addActionListener(menuItemListener); pasteMenuItem.addActionListener(menuItemListener); editMenu.add(cutMenuItem); editMenu.add(copyMenuItem); editMenu.add(pasteMenuItem); mainFrame.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { editMenu.show(mainFrame, e.getX(), e.getY()); } }); mainFrame.add(editMenu); mainFrame.setVisible(true); } class MenuItemListener implements ActionListener { public void actionPerformed(ActionEvent e) { statusLabel.setText(e.getActionCommand() + " MenuItem clicked."); } } }
THE OUTPUT
ANOTHER JPOPUPMENU EXAMPLE
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.border.BevelBorder; import javax.swing.event.PopupMenuEvent; import javax.swing.event.PopupMenuListener; public class PopupMenuExample extends JPanel { public JPopupMenu popup; public PopupMenuExample() { popup = new JPopupMenu(); ActionListener menuListener = new ActionListener() { public void actionPerformed(ActionEvent event) { System.out.println("Popup menu item [" + event.getActionCommand() + "] was pressed."); } }; JMenuItem item; popup.add(item = new JMenuItem("Left", new ImageIcon("1.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Center", new ImageIcon("2.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Right", new ImageIcon("3.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.add(item = new JMenuItem("Full", new ImageIcon("4.gif"))); item.setHorizontalTextPosition(JMenuItem.RIGHT); item.addActionListener(menuListener); popup.addSeparator(); popup.add(item = new JMenuItem("Settings . . .")); item.addActionListener(menuListener); popup.setLabel("Justification"); popup.setBorder(new BevelBorder(BevelBorder.RAISED)); popup.addPopupMenuListener(new PopupPrintListener()); addMouseListener(new MousePopupListener()); } // An inner class to check whether mouse events are the popup trigger class MousePopupListener extends MouseAdapter { public void mousePressed(MouseEvent e) { checkPopup(e); } public void mouseClicked(MouseEvent e) { checkPopup(e); } public void mouseReleased(MouseEvent e) { checkPopup(e); } private void checkPopup(MouseEvent e) { if (e.isPopupTrigger()) { popup.show(PopupMenuExample.this, e.getX(), e.getY()); } } } // An inner class to show when popup events occur class PopupPrintListener implements PopupMenuListener { public void popupMenuWillBecomeVisible(PopupMenuEvent e) { System.out.println("Popup menu will be visible!"); } public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { System.out.println("Popup menu will be invisible!"); } public void popupMenuCanceled(PopupMenuEvent e) { System.out.println("Popup menu is hidden!"); } } public static void main(String s[]) { JFrame frame = new JFrame("Popup Menu Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(new PopupMenuExample()); frame.setSize(300, 300); frame.setVisible(true); } }
OUTPUT:
COMMENT TO SHOW APPRECIATION
No comments:
Post a Comment