Skip to content
Advertisement

How can I call the mouse right click event in SwingGui?

I have a table. If I right-click I got a JPopUpMenu but before the pop-up I want to select the row where the right-click event is done. Here is what I’ve tried.

JavaScript

In that event, I cannot get any output from the console when I right-click. However, when I left-click, points are printed to the console.

java.awt.Point[x=105,y=76]

So, this event only works when I left-click. How can I make this event work with right-click?

Advertisement

Answer

Since you want custom mouse behavior, you should not use setComponentPopupMenu.

Instead, display the JPopupMenu yourself, using JPopupMenu’s show method:

JavaScript

You must check the MouseEvent in both mousePressed and mouseReleased, because exactly when a context menu is triggered depends on the platform and the look-and-feel. (Checking in mouseClicked may or may not be necessary, but it doesn’t hurt.)

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement