MediumPopupMenuSample.java Source code

Java tutorial

Introduction

Here is the source code for MediumPopupMenuSample.java

Source

/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski       
ISBN: 1-893115-78-X
Publisher: APress
*/

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class MediumPopupMenuSample {

    public static void main(String args[]) {
        // Define ActionListener
        ActionListener aListener = new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                System.out.println("Selected: " + event.getActionCommand());
            }
        };

        // Define PopupMenuListener
        PopupMenuListener pListener = new PopupMenuListener() {
            public void popupMenuCanceled(PopupMenuEvent event) {
                System.out.println("Canceled");
            }

            public void popupMenuWillBecomeInvisible(PopupMenuEvent event) {
                System.out.println("Becoming Invisible");
            }

            public void popupMenuWillBecomeVisible(PopupMenuEvent event) {
                System.out.println("Becoming Visible");
            }
        };

        // Define
        JFrame frame = new JFrame("Popup Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPopupMenu.setDefaultLightWeightPopupEnabled(false);

        // Create popup menu, attach popup menu listener
        final JPopupMenu popupMenu = new JPopupMenu();
        popupMenu.addPopupMenuListener(pListener);

        // Cut
        JMenuItem cutItem = new JMenuItem("Cut");
        cutItem.addActionListener(aListener);
        popupMenu.add(cutItem);

        // Copy
        JMenuItem copyItem = new JMenuItem("Copy");
        copyItem.addActionListener(aListener);
        popupMenu.add(copyItem);

        // Paste
        JMenuItem pasteItem = new JMenuItem("Paste");
        pasteItem.addActionListener(aListener);
        pasteItem.setEnabled(false);
        popupMenu.add(pasteItem);

        // Separator
        popupMenu.addSeparator();

        // Find
        JMenuItem findItem = new JMenuItem("Find");
        findItem.addActionListener(aListener);
        popupMenu.add(findItem);

        // Enable showing
        MouseListener mouseListener = new JPopupMenuShower(popupMenu);
        frame.addMouseListener(mouseListener);

        Button button = new Button("Label");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println(popupMenu.isLightWeightPopupEnabled());
            }
        });
        frame.getContentPane().add(button, BorderLayout.SOUTH);

        frame.setSize(350, 250);
        frame.setVisible(true);
    }
}

class JPopupMenuShower extends MouseAdapter {

    private JPopupMenu popup;

    public JPopupMenuShower(JPopupMenu popup) {
        this.popup = popup;
    }

    private void showIfPopupTrigger(MouseEvent mouseEvent) {
        if (popup.isPopupTrigger(mouseEvent)) {
            popup.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY());
        }
    }

    public void mousePressed(MouseEvent mouseEvent) {
        showIfPopupTrigger(mouseEvent);
    }

    public void mouseReleased(MouseEvent mouseEvent) {
        showIfPopupTrigger(mouseEvent);
    }
}