Hi Everyone,
I have a project that requires to use mouse right click. Upon right click, it should display a list of categories(drop down). How Do I do I use right click?
Many thanks,
Petra
Hi Everyone,
I have a project that requires to use mouse right click. Upon right click, it should display a list of categories(drop down). How Do I do I use right click?
Many thanks,
Petra
For Swing, do not rely on mouseClicked or testing for button 3 to show a context menu. The portable trigger for a popup menu is e.isPopupTrigger(), and it may fire on mousePressed on some platforms and mouseReleased on others (e.g., Windows). Handle both to ensure it always works, including Ctrl+click on macOS.
Example:
JPopupMenu popup = buildPopup(); // create and populate your JPopupMenu
MouseAdapter popupListener = new MouseAdapter() {
private void maybeShow(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
@Override public void mousePressed(MouseEvent e) { maybeShow(e); }
@Override public void mouseReleased(MouseEvent e) { maybeShow(e); }
};
targetComponent.addMouseListener(popupListener); Alternatively, since Java 6 you can simply call targetComponent.setComponentPopupMenu(popup) and skip a custom listener; Swing will manage the cross-platform trigger for you.
If switching from a JMenuBar to a JPopupMenu and the popup does not appear:
setComponentPopupMenu(...) with separate manual show(...) logic on the same component unless you have a reason; pick one approach.See Oracle’s guides: Bringing up a popup menu and How to write a Mouse Listener.
Jump to Post— mKorbel 274
add MouseListener to container (JPanel???)
override public void mouseClicked(MouseEvent e) { then there are two ways
panel.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getModifiers() == MouseEvent.BUTTON3_MASK && e.getClickCount() == 1) { // whatever } } });or
panel.addMouseListener(new MouseAdapter() …
Jump to Post— Lucaci Andrew 140Here's a small example. You can use the
getButton()function fromMouseEvent.final JLabel lblRightClickMe = new JLabel("Right click me"); //create a label lblRightClickMe.setBounds(152, 119, 94, 14); final JPopupMenu jpm = new JPopupMenu("Hello"); //create a JPopupMenu jpm.add("Right");jpm.add("Clicked");jpm.add("On"); //add some elements jpm.add("This");jpm.add("Label"); lblRightClickMe.setComponentPopupMenu(jpm); //set jpm as …
add MouseListener to container (JPanel???)
override public void mouseClicked(MouseEvent e) { then there are two ways
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getModifiers() == MouseEvent.BUTTON3_MASK && e.getClickCount() == 1) {
// whatever
}
}
});
or
panel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e) && e.getClickCount() == 1) {
// whatever
}
}
});
i think you have to use mouseadapter class and then you have to use mouseclicked event to handle right click of mouse.
Here's a small example. You can use the getButton() function from MouseEvent.
final JLabel lblRightClickMe = new JLabel("Right click me"); //create a label
lblRightClickMe.setBounds(152, 119, 94, 14);
final JPopupMenu jpm = new JPopupMenu("Hello"); //create a JPopupMenu
jpm.add("Right");jpm.add("Clicked");jpm.add("On"); //add some elements
jpm.add("This");jpm.add("Label");
lblRightClickMe.setComponentPopupMenu(jpm); //set jpm as this label's popup menu
lblRightClickMe.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
if (arg0.getButton() == MouseEvent.BUTTON3){ //get the mouse button
jpm.show(arg0.getComponent(), arg0.getX(), arg0.getY());//set the position and show the popup menu
}
}
});
Many thanks to all, I shall do your suggestions.
My popup menu works now. My standard menu as well. But when I switch from standard JMenu to JPopupMenu, it doesnt display my pop up menu. How? Many thanks.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.