Java JPopupMenu

Java JPopupMenu

Java JPopupMenu
Java JPopupMenu always pop up at any position of a component.JPopup Menu inherits from JComponent class.

How to declare Java JPopupMenu

[class JPopupMenu extends JComponent implements Accessible, MenuElement]


JPopupMenu constructors

  • JPopupMenu()-constructs JPopupMenu with no invoker.
  • JPopupMenu(String s)-constructs JPopupMenu with a title.


Java JPopupMenu Example

[
import javax.swing.*;
import java.awt.event.*;
class PopupMenuExample
{
    PopupMenuExample(){
        final JFrame frame= new JFrame("PopupMenu Example");
        final JPopupMenu menu = new JPopupMenu("Edit");
        JMenuItem cut = new JMenuItem("Cut");
        JMenuItem copy = new JMenuItem("Copy");
        JMenuItem paste = new JMenuItem("Paste");
        menu.add(cut); menu.add(copy); menu.add(paste);
        frame.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                menu.show(frame , e.getX(), e.getY());
            }
        });
        frame.add(menu);
        frame.setSize(300,300);
        frame.setLayout(null);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public static void main(String args[])
    {
        new PopupMenuExample();
    }}  
]

Java JPopupMenu Example with MouseListener and ActionListener

[
import javax.swing.*;
import java.awt.event.*;
class PopupMenuExample
{
    PopupMenuExample(){
        final JFrame frame= new JFrame("PopupMenu Example");
        final JPopupMenu menu = new JPopupMenu("Edit");
        final JTextArea area=new JTextArea();
        area.setSize(200,200);
        area.setBounds(50,50,200,200);
        frame.add(area);
        JMenuItem cut = new JMenuItem("Cut");
        JMenuItem copy = new JMenuItem("Copy");
        JMenuItem paste = new JMenuItem("Paste");
        JMenuItem selectAll = new JMenuItem("Select All");
        cut.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                area.cut();
            }
            {

            }
        });

        copy.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                area.copy();
            }
        });

        paste.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                area.paste();
            }
        });
        selectAll.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                area.selectAll();
            }
        });



        menu.add(cut); menu.add(copy); menu.add(paste);
        menu.add(selectAll);
        frame.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                menu.show(frame , e.getX(), e.getY());
            }
        });
        frame.add(menu);
        frame.setSize(400,400);
        frame.setLayout(null);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    public static void main(String args[])
    {
        new PopupMenuExample();
    }}  
]

Output

Java JPopupMenu Example with MouseListener and ActionListener


Code Explanation

  • [final JPopupMenu menu = new JPopupMenu("Edit");]-creates JPopupMenu with a string label.
  • [final JFrame frame= new JFrame("PopupMenu Example");]-creates a JFrame window.
  • [final JTextArea area=new JTextArea();]-creates JTextArea.

Code that performs JTextArea Actions

Code [cut.addActionListener( new ActionListener()
{
    public void actionPerformed(ActionEvent event)
    {
        area.cut();
    }
    {

    }
});] performs cut action on JTextArea.

Code [selectAll.addActionListener(new ActionListener()
        {
public void actionPerformed(ActionEvent event)
        {
        area.selectAll();
        }
        });] adds Anonymous class ActionListener to selectAll menu Item.

Other lessons you should also Learn.

How to add JMenuItem to JMenu

How to Implement ActionListener Interface




COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Java JPopupMenu
Java JPopupMenu
Java JPopupMenu
https://4.bp.blogspot.com/-JhA4lDyxnY4/WM5PGK_hhOI/AAAAAAAAHPM/UKUIwO8QQnE9VYP4Irmw6IHyTlu0BBnvQCEw/s200/Java-JPopupMenu-Example-min.PNG
https://4.bp.blogspot.com/-JhA4lDyxnY4/WM5PGK_hhOI/AAAAAAAAHPM/UKUIwO8QQnE9VYP4Irmw6IHyTlu0BBnvQCEw/s72-c/Java-JPopupMenu-Example-min.PNG
Java Codes
http://www.download-all.net/2017/03/java-jpopupmenu.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/03/java-jpopupmenu.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy