Introduction to JPopupMenu
JPopupMenu is a Swing component that shows a popup menu in the application. The Popup menu is a small window that pops up and offers a series of choices on any event, like a button click.
Example
- The edit menu may contain options like cut, copy and paste.
- The menu may contain options like Home, contact us, about us, inquire, etc.
JPopupMenu is a class that is used in a Swing application. Here is the declaration:
public class JPopupMenu extends JComponent implements Accessible, MenuElement
JComponent base class for all Swing components except top-level containers.
Constructors
- JPopupMenu(): This constructor constructs a JPopupMenu without an invoker.
- JPopupMenu(String label): This constructor constructs a JPopupMenu with a title.
Example of JPopupMenu
Let’s understand JPopupMenu with the help of an example:
package application;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingConstants;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
public class JPopupMenuDemo {
public static void main(String[] args) {
JFrame app = new JFrame();
JButton organization = new JButton("Organization");
app.add(organization);
app.setSize(400, 400);
app.setLayout(new GridLayout(2, 1));
app.setVisible(true);
organization.setVisible(true);
JPopupMenu menu = new JPopupMenu("Menu");
JLabel status = new JLabel();
status.setHorizontalTextPosition(SwingConstants.CENTER);
status.setVerticalTextPosition(SwingConstants.CENTER);
app.add(status);
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
status.setText(e.getActionCommand());
status.setVisible(true);
}
};
JMenuItem enquire = new JMenuItem("Enquire");
enquire.addActionListener(listener);
JMenuItem aboutUs = new JMenuItem("About Us");
aboutUs.addActionListener(listener);
JMenuItem home = menu.add("Home");
home.addActionListener(listener);
JMenuItem contactUs = menu.add(new AbstractAction() {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
status.setText(e.getActionCommand());
status.setVisible(true);
}
});
menu.add(enquire);
menu.add(aboutUs);
menu.add(contactUs);
home.setText("Home. Index - " + menu.getComponentIndex(home));
enquire.setText("Enquire. Index - " + menu.getComponentIndex(enquire));
aboutUs.setText("About Us. Index - " + menu.getComponentIndex(aboutUs));
contactUs.setText("Contact Us. Index - " + menu.getComponentIndex(contactUs));
menu.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
status.setText("Popup menu is visible now.");
status.setVisible(true);
}
@Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
status.setText("Popup menu is invisible now.");
}
@Override
public void popupMenuCanceled(PopupMenuEvent e) {
status.setText("Popup menu is cancelled now.");
}
});
organization.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
menu.show(app, e.getX(), e.getY());
}
});
}
}
Explanation:
This application is for any organization with basic pages like Home, enquire, about us, and contact us.
- First, we have created a JFrame of size 400 by 400 pixels. JFrame is a top-level window with a title and border.
- We added an “Organization” button to show the application’s main menu.
- A label is created to show the status of the application (Which menu item is clicked)
- We create a popup menu using the JPopupMenu class and add four menu items: Home, Enquire, About Us, and Contact Us.
- The index of the menu item is figured out and shown using the function “getComponentIndex.”
- Added a separator at the end of the menu using the function “addSeparator.”
- We add a listener to show the menu window when clicking a button.
- When a menu pops up, we add a listener that changes the status when menu items are clicked.
- When the user clicks the “Organization” button, a popup window with menu items opens.
- When a menu item is clicked, “status” is changed.
Output:
- This is the application:
- When the user clicks on “Organization,” a popup menu is shown:
- When the user clicks on “Home”:
- When the user clicks on “Enquire”:
- When the user clicks on “About Us”:
- When the user clicks on “Contact Us”:
Methods of JPopupMenu
Some of the useful JPopupMenu methods:
- JMenuItem add(Action a): This method adds a menu item at the end of the menu, and the action for the menu item is specified.
- JMenuItem add(JMenuItem menuItem): This method also adds a menu item at the end, except this menu item’s action must be specified in JMenuItem.
- JMenuItemadd(String s): This method creates a JMenuItem, adds it to the menu, and returns it for further configuration.
- void addSeparator(): This method adds a separator space at the end of the menu.
- int getComponentIndex(Component c): This method returns the index of the specified component within this JPopupMenu.
- static boolean getDefaultLightWeightPopupEnabled(): This method returns the value of the defaultLightWeightPopupEnabled property, which by default is true.
- static void setDefaultLightWeightPopupEnabled(boolean aFlag): This method sets the value of the defaultLightWeightPopupEnabled property.
- String getLabel(): This method returns the label of this menu.
- void setLabel(String label): This method sets the label for this menu.
- Insets getMargin(): This method returns the margin, in pixels, between the border of this menu and its containers.
- boolean isLightWeightPopupEnabled(): This method returns the property “lightWeightPopupEnabled.”
- void setLightWeightPopupEnabled(boolean aFlag): This method sets the property “lightWeightPopupEnabled”, which by default is true.
- boolean isVisible(): This method returns the current visibility of this menu.
- void setVisible(boolean b): This method sets the visibility of the menu.
- protected String paramString(): This method returns a string representation of this menu.
- void remove(int pos): This method removes the component at the specified index from this menu.
- void setLocation(int x, int y): This method sets the location of the top left corner of this menu using x and y coordinates.
- void setPopupSize(int width, int height): This method sets the size of this menu window to the specified height and width.
- void setPopupSize(Dimension d): This method sets the size of this menu window to the defined dimension.
- PopupMenuUI getUI(): This method returns the look and feels of this menu.
- void setUI(PopupMenuUI ui): This method sets the look and feels of this menu.
- void updateUI(): This method resets the look and feel of this menu.
- void insert(Action a, int index): This method inserts the menu item with specified action at the specified index.
- void insert(Component component, int index): This method inserts the menu item in the menu at the selected index.
- invalid show(Component invoker, int x, int y): This method shows the menu in the component invoker at the position x, y in the coordinate space.
- void addPopupMenuListener(PopupMenuListener l): This method adds a listener to this popup menu, with the help of which we can check when the popup menu is visible, invisible, and canceled.
Recommended Articles
This is a guide to JPopupMenu. Here we discuss the introduction, example, and methods of JPopupMenu, respectively. You may also have a look at the following articles to learn more –