Java JPopupMenu ensurePopupIsOnScreen(JPopupMenu popup)

Here you can find the source of ensurePopupIsOnScreen(JPopupMenu popup)

Description

ensure Popup Is On Screen

License

Open Source License

Declaration

public static void ensurePopupIsOnScreen(JPopupMenu popup) 

Method Source Code

//package com.java2s;

import javax.swing.JPopupMenu;

public class Main {
    public static void ensurePopupIsOnScreen(JPopupMenu popup) {
        java.awt.Point location = popup.getLocation(null);
        java.awt.Dimension size = popup.getSize(null);
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        screenSize.height -= 28; // hack for standard Windows Task Bar

        javax.swing.SwingUtilities.convertPointToScreen(location, popup);

        if (location.x < 0) {
            location.x = 0;//from   w ww  . j a  va2  s  .com
        } else if (location.x + size.width > screenSize.width) {
            location.x -= (location.x + size.width) - screenSize.width;
        }
        if (location.y < 0) {
            location.y = 0;
        } else if (location.y + size.height > screenSize.height) {
            location.y -= (location.y + size.height) - screenSize.height;
        }

        popup.setLocation(location);
    }
}

Related

  1. attachPopupMenu(final JComponent component, final JPopupMenu popupMenu)
  2. cleanPopupLayoutMetrics(JPopupMenu popupMenu)
  3. computePopupLocation(MouseEvent event, Component rel, JPopupMenu popup)
  4. createPopupMenu()
  5. createPopupMenu(String menuText, JPopupMenu menu, ActionListener listener)
  6. findMenuComponent(JPopupMenu menu, String menuComponentName)
  7. findSubMenu(JPopupMenu popupMenu, String name)
  8. getMousePopupAdapter(final Component component, final JPopupMenu popup)
  9. getOnlyPopupMenu(Container owner)