Swing PopUp Menu

We have seen popup menu at desktop based user interface for programs. The popup menu acts like shortcut links. They are generally setup for the performing actions, that user might need often, like ‘Refresh’, ‘New’ for operation system. Popup menu are easy to access (just by right click of mouse) and delivers most commonly needed features to user. Mostly every program now a days have there own popup menu. Today, we will create a simple popup menu based on java swing with some menu items and see how it looks.


        MouseListener popupListener = new PopupListener(popup);
        popupMenuEx.centerJPanel.addMouseListener(popupListener);
        // Add more according to requirements
        . . .

Assign popup menu.

Below is the full code for creating popup menu class :

package swing.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;

/**
 *
 * @author prajin
 */
public class Popup implements ActionListener, ItemListener {

    private PopupMenuEx popupMenuEx;

    public Popup(PopupMenuEx mf) {
        popupMenuEx = mf;
        try {
            createAndShowPopupMenu();
        } catch (Exception e) {
        }
    }

    public void createAndShowPopupMenu() {
        JPopupMenu popup = new JPopupMenu(title);

        JMenuItem newMenu = new JMenuItem(" New ");
        JMenuItem deleteMenu = new JMenuItem(" Delete ");
        JMenuItem saveMenu = new JMenuItem(" Save ");
        JMenuItem searchMenu = new JMenuItem(" Search ");
        JMenuItem resetMenu = new JMenuItem(" Exit ");
        JSeparator separator1 = new JSeparator();
        JSeparator separator2 = new JSeparator();

        JMenu optionMenu = new JMenu("Option");
        optionMenu.add(deleteMenu);
        optionMenu.add(searchMenu);

        popup.add(newMenu);
        popup.add(saveMenu);
        popup.add(separator1);
        popup.add(optionMenu);
        popup.add(separator2);
        popup.add(resetMenu);

        MouseListener popupListener = new PopupListener(popup);
        popupMenuEx.centerJPanel.addMouseListener(popupListener);

        newMenu.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
            }
        });
    }

    public void actionPerformed(ActionEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void itemStateChanged(ItemEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

class PopupListener extends MouseAdapter {

    JPopupMenu popup;

    PopupListener(JPopupMenu popupMenu) {
        popup = popupMenu;
    }

    public void mousePressed(MouseEvent e) {
        maybeShowPopup(e);
    }

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

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


Below is the code for using popup menu :

package swing.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

/**
 *
 * @author prajin
 */
public class PopupMenuEx {

    public JFrame jFrame;
    public JPanel centerJPanel;
    Popup popup;

    public PopupMenuEx() {
        initUIComponents();
    }

    public void initUIComponents() {
        jFrame = new JFrame("Popup Menu Ex");
        jFrame.pack();
        centerJPanel = new JPanel();
        centerJPanel.setBorder(new LineBorder(Color.DARK_GRAY, 4));

        jFrame.add(centerJPanel, BorderLayout.CENTER);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setSize(300, 250);
        popup = new Popup(this);
        jFrame.setVisible(true);
    }

    public static void main(String args[]) {
        new PopupMenuEx();
    }
}

The output looks like this :

Swing based JPopup Menu

Leave a comment

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.