public Point setPopUpMenuLocation(Point p, Dimension d) {
        Rectangle bounds = getSafeScreenBounds(p);
        int x = p.x;
        int y = p.y;
        if (y < bounds.y) {
            y = bounds.y;
        } else if (y > bounds.y + bounds.height) {
            y = bounds.y + bounds.height;
        }
        if (x < bounds.x) {
            x = bounds.x;
        } else if (x > bounds.x + bounds.width) {
            x = bounds.x + bounds.width;
        }

        if (x + d.width > bounds.x + bounds.width) {
            x = (bounds.x + bounds.width) - d.width;
        }
        if (y + d.height > bounds.y + bounds.height) {
            y = (bounds.y + bounds.height) - d.height;
        }
        return new Point(x,y);
    }