Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8231047

JPopupMenu is hidden by Windows Task Bar

    Details

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10

      NOTE: It works on macOS.

      A DESCRIPTION OF THE PROBLEM :
      JPopupMenu.show(Component invoker, int x, int y) is supposed to ensure that the popup menu is displayed within the screen bounds, so that the entire popup menu is visible, even if it was invoked along side the screen edge.

      Here's someone reporting the same issue on stackoverflow with screenshots for illustration:
      https://stackoverflow.com/questions/50763440/prevent-jpopupmenu-from-being-covered-by-task-bar


      Investigation:

      When JPopupMenu.show(Component invoker, int x, int y) is called, it will internally call javax.swing.JPopupMenu.adjustPopupLocationToFitScreen(int, int) to ensure that the popup menu is displayed within screen bounds, and within screen insets.

      This works on macOS, but not Windows 10. On macOS, the popup is adjust for screen bounds and screen insets, but on Windows 10 it's only adjusted for screen bounds but not screen insets (i.e. Task Bar) because javax.swing.JPopupMenu.canPopupOverlapTaskBar() return true on Windows 10. Internally, sun.awt.SunToolkit.canPopupOverlapTaskBar() is called which check for the SET_WINDOW_ALWAYS_ON_TOP_PERMISSION. So I guess the idea is to use always on top to display the popup on top of the task bar, but this doesn't work (because always on top is never set for the popup window) and even if it were to be set, Windows still wouldn't necessarily display the popup on top (it might sometimes, but it won't always).


      SOLUTION:

      Do not rely on setAlwaysOnTop() since it doesn't work reliable anyway due to Windows. Instead just display the popup within visible screen bounds and screen insets.

      TL;DR javax.swing.JPopupMenu.canPopupOverlapTaskBar() should always return false on Windows 10.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Right-click component that is located a few pixels above the task bar and notice the popup menu being displayed behind the task bar.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The entire popup menu should be visible.

      TL;DR javax.swing.JPopupMenu.canPopupOverlapTaskBar() should always return false on Windows 10.
      ACTUAL -
      The popup menu should be displayed above the task bar (i.e. by adjusting y coordinate, not by setting always on top, which doesn't work)

      ---------- BEGIN SOURCE ----------
      Here's an example from a 3rd party:
      https://stackoverflow.com/questions/50763440/prevent-jpopupmenu-from-being-covered-by-task-bar
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Manually adjust popup location before calling JDK methods to show the popup.

      FREQUENCY : always


        Attachments

        1. PopBody.java
          2 kB
        2. popup.png
          popup.png
          157 kB
        3. Test.java
          3 kB

          Activity

            People

            • Assignee:
              psadhukhan Prasanta Sadhukhan
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: