1. Introduction to AWT PopupMenu
In our past examples, we equipped the AWT Menu in the menu-bar. But we can also display the Menu wherever we need inside a top-level container. The Top-Level container can be a Frame, Dialog or even a Java Applet. Now, have a look at the below picture:
Here, we can see a AWT Menu with three MenuItems in it. We call this as a Pop-up Menu as it is directly owned by the top-level container. Here, in our case, it is an AWT Frame Window. The show method of the AWT PopupMenu class will take three parameters. First one is the parent which owns the Menu. The parent can be a Frame, a Dialog or an Applet. So just like how we add standard components like TextField, Label, CheckBox to the Top-level window, we can add the PopupMenu also to its parent. Second and third arguments tell where we want to display the Menu along with its menu items.
2. About the Example
Below is the PupupMenu we will create in this example:

Here, we have a PopupMenu with 4 MenuItems and one separator. We can also notice that the Menu is not attached to a Menubar, and it is displayed in the AWT Frame Window. In this example, we will display the Menu when you right click the mouse inside the AWT Frame.
3. Prepare MenuItems for PopupMenu
Our example requires four MenuItem
instances in the PopupMenu
. Hence, we create all these four menu items and code is below:
1 2 3 4 5 |
//Sample 01: Create MenuItems for Pop-up Menu popLGray = new MenuItem("Light Gray"); popMGray = new MenuItem("Gray"); popDGray = new MenuItem("Dark Gray"); popReset = new MenuItem("Reset"); |
In our example, we need to change the background colour when the user clicks on the menu item. So, we will tie each MenuItem with an action command via setActionCommand
method.
1 2 3 4 5 |
//Sample 02: Set Action Commands popLGray.setActionCommand("LG"); popMGray.setActionCommand("MG"); popDGray.setActionCommand("DG"); popReset.setActionCommand("R"); |
4. Create The AWT PopupMenu
The
PopupMenu class of Java AWT is useful for creating the pop-up menus. Like other standard AWT component, we can add this class to the container class via the add
method. In the below code, we store the AWT PopupMenu instance in the reference, popup
. Then, we call the
add method of the PopupMenu
to add the Menu Items to it. Note, at line 6, we also added a separator to our pop-up menu.
1 2 3 4 5 6 7 |
//Sample 03: Create Pop-up Menu popup = new PopupMenu(); popup.add(popLGray); popup.add(popMGray); popup.add(popDGray); popup.addSeparator(); popup.add(popReset); |
5. AWT PopupMenu & MouseListener
We will display the PopupMenu in the AWT Frame Window’s location where the user clicks the right mouse button. So, our frame window implements MouseListener to track the mouse click.
1 2 3 4 5 6 |
//Sample 04: Implement Mouse Listener public class FrameWin extends Frame implements WindowListener, MouseListener { |
Since the frame class claims that it will serve handler for listening the MouseEvents, we sign up our AWT Frame with the MouseListener using addMouseListener
method. Also note the add method call which adds the PopupMenu as a component to the Frame Window. We require this as we need to show the pop-up menu when user right clicks on the Frame window area.
1 2 3 |
//Sample 05: Register with Mouse Listener add(popup); //=> Most developer may miss this addMouseListener(this); |
6. Show Pop-up Menu
To display the pop-up menu, we should call the
show method of the PopupMenu
instance. This method requires three parameters. They are:
- Parent. We pass this as a first argument and in our case, we pass ‘this’ reference as Frame is the parent window of the pop-up menu.
- X Co-ordinate value which we can get from the MouseEvent e by calling
getX
method. - Y Co-ordinate value which we can get from the MouseEvent e by calling
getY
method.
In the below code, first we check user clicked a right mouse button by checking the constant BUTTON3
with the getButton
method call’s return value. Then, we display the pop-up menu at the mouse cursor location by calling
show method of the AWT PopupMenu
instance.
1 2 3 4 5 6 |
public void mouseClicked(MouseEvent e) { //Sample 06: Show Pop-up Menu if (e.getButton() == MouseEvent.BUTTON3) popup.show(this, e.getX(), e.getY()); } |
7. Handle Popup Menu Item Events
To handle the click event of menu items present in the pop-up menu, the frame window implements the ActionListener
. The code is below:
1 2 3 4 5 6 |
//Sample 07: Implement Action Listener public class FrameWin extends Frame implements WindowListener, MouseListener, ActionListener |
Next, all the MenuItems of our pop-up menu enroll with the ActionListener
which is nothing but our frame window. After the call to addActionListener
, all four menu items will route the ActionEvent
to our Frame Window.
1 2 3 4 5 |
//Sample 08: Register With Action Listener popLGray.addActionListener(this); popMGray.addActionListener(this); popDGray.addActionListener(this); popReset.addActionListener(this); |
Next, we override the actionPerformed handler for our AWT Frame derived class. From ActionEvent e, we get the action command by calling the getActionCommand and form a switch…case based on the outcome. The case body will set the correct background color for the Frame window by making use the setBackground method call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
@Override public void actionPerformed(ActionEvent e) { //Sample 09: Get the Action for Switch Case String action = e.getActionCommand(); switch (action) { case"LG": setBackground(Color.LIGHT_GRAY); break; case"MG": setBackground(Color.GRAY); break; case"DG": setBackground(Color.DARK_GRAY); break; case"R": setBackground(Color.WHITE); break; } } |
8. AWT PopupMenu Explained – Youtube Video
9. Code Reference
FrameWin.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
import java.awt.Color; import java.awt.Component; import java.awt.Frame; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //Sample 04: Implement Mouse Listener //Sample 07: Implement Action Listener public class FrameWin extends Frame implements WindowListener, MouseListener, ActionListener { //Sample 00: Class Members PopupMenu popup; MenuItem popLGray; MenuItem popMGray; MenuItem popDGray; MenuItem popReset; public FrameWin(String FrameTitle) { //Display the Frame Window super(FrameTitle); setSize(400, 400); setLocation(100,100); addWindowListener(this); //Sample 01: Create MenuItems for Pop-up Menu popLGray = new MenuItem("Light Gray"); popMGray = new MenuItem("Gray"); popDGray = new MenuItem("Dark Gray"); popReset = new MenuItem("Reset"); //Sample 02: Set Action Commands popLGray.setActionCommand("LG"); popMGray.setActionCommand("MG"); popDGray.setActionCommand("DG"); popReset.setActionCommand("R"); //Sample 03: Create Pop-up Menu popup = new PopupMenu(); popup.add(popLGray); popup.add(popMGray); popup.add(popDGray); popup.addSeparator(); popup.add(popReset); //Sample 05: Register with Mouse Listener add(popup); //=> Most developer may miss this addMouseListener(this); //Sample 08: Register With Action Listener popLGray.addActionListener(this); popMGray.addActionListener(this); popDGray.addActionListener(this); popReset.addActionListener(this); } public void windowOpened(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} public void windowClosing(WindowEvent e) { this.dispose(); } public void mouseClicked(MouseEvent e) { //Sample 06: Show Pop-up Menu if (e.getButton() == MouseEvent.BUTTON3) popup.show(this, e.getX(), e.getY()); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} @Override public void actionPerformed(ActionEvent e) { //Sample 09: Get the Action for Switch Case String action = e.getActionCommand(); switch (action) { case"LG": setBackground(Color.LIGHT_GRAY); break; case"MG": setBackground(Color.GRAY); break; case"DG": setBackground(Color.DARK_GRAY); break; case"R": setBackground(Color.WHITE); break; } } } |
Categories: AWT