Java Swing - Adjusting Right Click JPopupMenu position when Shift + F10 is Pressed

[Last Updated: Dec 26, 2018]

On operating system level (Windows and Linux), the Shift+F10 key is a keyboard shortcut for calling up the context menu on the selected item. Programs should not try to register custom keys for invoking context menus.

Following example assigns a JPopup to JTree (via JComponent.setComponentPopupMenu()), so when JTree is in the focus and we press Shift + F10 then popup should show up. But it can show anywhere on the JTree. We can programmatically set a desired location before the popup shows up.


public class ExampleMain {

  public static void main(String[] args) {
      JTree tree = new JTree(new Hashtable<>(createTreeData()));

      JPopupMenu popupMenu = new JPopupMenu();
      popupMenu.add(new JMenuItem("Example Item"));

      initPopupListener(tree, popupMenu);

      JFrame frame = createFrame();
      tree.setPreferredSize(new Dimension(200, tree.getPreferredSize().height));
      frame.add(new JScrollPane(tree), BorderLayout.WEST);

  private static void initPopupListener(JTree tree, JPopupMenu popupMenu) {
      popupMenu.addPopupMenuListener(new PopupMenuListener() {
          public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
              //get selected node's rectangle
              Rectangle rect = tree.getPathBounds(tree.getSelectionPath());
     -> c.setEnabled(rect != null));
              if (rect == null) {

              Point p = new Point(rect.x + rect.width / 2, rect.y + rect.height);
              SwingUtilities.convertPointToScreen(p, tree);
              popupMenu.setLocation(p.x, p.y);

          public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {


          public void popupMenuCanceled(PopupMenuEvent e) {


  private static Map<?, ?> createTreeData() {
      return Map.of("Sports",
              new String[]{"Mustang", "Corvette", "Ferrari"},
              new String[]{"BMW", "Mercedes-Benz", "Rolls-Royce"});

  private static JFrame createFrame() {
      JFrame frame = new JFrame("Popup On Shift + F10 Press");
      frame.setSize(new Dimension(500, 400));
      return frame;


On selecting a node and pressing SHIFT + F10:

Example Project

Dependencies and Technologies Used:

  • JDK 11
  • Maven 3.5.4

Invoking mouse right click menu via Shift + F10 Select All Download
  • jpopup-invoking-via-system-shortcut
    • src
      • main
        • java
          • com
            • logicbig
              • example

    See Also