Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's an interesting question, because it highlights missing api on JComponent :-) </p> <p>As we all know, the recommended way to register popupMenus is to use the componentPopupMenu property. Related api is </p> <pre><code> void setComponentPopupMenu(JPopupMenu); JPopupMenu getComponentPopupMenu(); Point getPopupLocation(MouseEvent); </code></pre> <p>what is missing (and actually needed for this requirement) is</p> <pre><code>JPopupMenu getComponentPopupMenu(MouseEvent); </code></pre> <p>this lack is all the more annoying, as the getPopupLocation is called (by AWTEventHelper deep in the LAF) <em>after</em> getComponentPopup(). So there's no leeway for a hack like storing the last mouse event which might have triggered the popup and then decide which/if to return popup. And returning null for the location will only result in showing it at the mouse location</p> <p>The only (dirty) hack (around my utter reluctance to get my hands dirty with a MouseListener ;-) is to override getComponentPopup and decide there whether or not to return it based on current mouse position</p> <pre><code> table = new JTable(model) { /** * @inherited &lt;p&gt; */ @Override public JPopupMenu getComponentPopupMenu() { Point p = getMousePosition(); // mouse over table and valid row if (p != null &amp;&amp; rowAtPoint(p) &gt;= 0) { // condition for showing popup triggered by mouse if (isRowSelected(rowAtPoint(p))) { return super.getComponentPopupMenu(); } else { return null; } } return super.getComponentPopupMenu(); } }; </code></pre> <p>the side-effect is that popup showing isn't triggered by keyboard as long as the mouse is anywhere above the table, which might or not be a problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload