// PopupMenuExample.java
// A simple example of JPopupMenu. (See MenuElementExample.java for another.)
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
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("left.gif")));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(menuListener);
popup.add(item = new JMenuItem("Center",
new ImageIcon("center.gif")));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(menuListener);
popup.add(item = new JMenuItem("Right", new
ImageIcon("right.gif")));
item.setHorizontalTextPosition(JMenuItem.RIGHT);
item.addActionListener(menuListener);
popup.add(item = new JMenuItem("Full", new ImageIcon("full.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);
}
}
|