Tuesday, August 7, 2012

Java PopupMenu

A popup menu to be used in the swing. This code is simplified. Can be used in a fnal class  or within a class of its own.


public class GPaises extends javax.swing.JInternalFrame {

        private void createPopupMenu() {
            try {
           

                //Create the popup menu wiht icons .
                JPopupMenu popup = new JPopupMenu();
                JMenuItem menuItem;
                menuItem = new JMenuItem("Cut, new ImageIcon(getClass().getResource("/gii/images/cut_red.png")));
                menuItem.setHorizontalTextPosition(JMenuItem.RIGHT);
                menuItem.addActionListener(menuListener);
                popup.add(menuItem);
                menuItem = new JMenuItem("Copy", new ImageIcon(getClass().getResource("/gii/images/page_copy.png")));
                menuItem.setHorizontalTextPosition(JMenuItem.RIGHT);
                menuItem.addActionListener(menuListener);
                popup.add(menuItem);
                menuItem = new JMenuItem("Paste", new ImageIcon(getClass().getResource("/gii/images/page_paste.png")));
                menuItem.setHorizontalTextPosition(JMenuItem.RIGHT);
                menuItem.addActionListener(menuListener);
                popup.add(menuItem);
                menuItem = new JMenuItem("Delete", new ImageIcon(getClass().getResource("/gii/images/page_delete.png")));
                menuItem.setHorizontalTextPosition(JMenuItem.RIGHT);
                menuItem.addActionListener(menuListener);
                popup.add(menuItem);
                popup.addSeparator();
                menuItem = new JMenuItem("Add", new ImageIcon(getClass().getResource("/gii/images/picture_add.png")));
                menuItem.setHorizontalTextPosition(JMenuItem.RIGHT);
                menuItem.addActionListener(menuListener);
                popup.add(menuItem);
                menuItem = new JMenuItem("Save", new ImageIcon(getClass().getResource("/gii/images/picture_save.png")));
                menuItem.setHorizontalTextPosition(JMenuItem.RIGHT);
                menuItem.addActionListener(menuListener);
                popup.add(menuItem);

                //Add listener to the text area so the popup menu can come up.
                MouseListener popupListener = new PopupListener(popup);
               
               
                //ActionListener the choice of the popup, the event will buy the name / description in JMenuItem
                ActionListener menuListener = new ActionListener() {
                    public void actionPerformed(ActionEvent event) {
                        if (event.getActionCommand().contentEquals("Cur")) {
                            copy();
                        } else if (event.getActionCommand().contentEquals("Copy")) {
                            copy();
                        } else if (event.getActionCommand().contentEquals("Paste")) {
                            paste();
                        } else if (event.getActionCommand().contentEquals("Delete")) {
                            clear();
                        } else if (event.getActionCommand().contentEquals("Add")) {
                            add();
                        } else if (event.getActionCommand().contentEquals("Save")) {
                            saveAs();
                        }
                    }

                    private void copy() { 
                        //Copy action
                    }

                    private void paste() {
                        //Paste action
                    }

                    private void clear() {
                        //Clear action
                    }

                    private void add() {
                       //Add action
                    }

                    private void saveAs() {
                        //Save ass action
                    }
                };
               
               

               
                //Mouse popup Componunet listener
                //Here we define which is the component that will invoke the popup
                jLIcon.addMouseListener(popupListener);
            } catch (Exception ex) {
                //Error Exception
            }
        }
       
       
       
         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());
            }
        }
    }
}
I accept any comment or improvement. We're always learning.


No comments:

Post a Comment