Java Code Examples for javax.swing.JPopupMenu#getPreferredSize()

The following examples show how to use javax.swing.JPopupMenu#getPreferredSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GUIGraphicsUtils.java    From tn5250j with GNU General Public License v2.0 6 votes vote down vote up
public static void positionPopup(Component component, JPopupMenu jpm,
		int xCoord , int yCoord) {

	Dimension popupSize = jpm.getSize();
	if(popupSize.width == 0)
		popupSize = jpm.getPreferredSize();
	Point point = new Point(xCoord + popupSize.width, yCoord + popupSize.height);
	SwingUtilities.convertPointToScreen(point, component);
	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	int x = 0;
	int y = 0;
	if(point.y > screenSize.height - 25)
		y = yCoord - popupSize.height;
	if(point.x > screenSize.width)
		x = xCoord - popupSize.width;
	jpm.show(component, x != 0 ? x : xCoord, y != 0 ? y : yCoord);
}
 
Example 2
Source File: GraphicUtils.java    From xyTalk-pc with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
    * Returns a point where the given popup menu should be shown. The point is
    * calculated by adjusting the X and Y coordinates so that the popup menu
    * will not be clipped by the screen boundaries.
    * 
    * @param popup
    *            the popup menu
    * @param x
    *            the x position in screen coordinate
    * @param y
    *            the y position in screen coordinates
    * @return the point where the popup menu should be shown in screen
    *         coordinates
    */
   public static Point getPopupMenuShowPoint(JPopupMenu popup, int x, int y) {
Dimension sizeMenu = popup.getPreferredSize();
Point bottomRightMenu = new Point(x + sizeMenu.width, y
	+ sizeMenu.height);

Rectangle[] screensBounds = getScreenBounds();
int n = screensBounds.length;
for (int i = 0; i < n; i++) {
    Rectangle screenBounds = screensBounds[i];
    if (screenBounds.x <= x
	    && x <= (screenBounds.x + screenBounds.width)) {
	Dimension sizeScreen = screenBounds.getSize();
	sizeScreen.height -= 32; // Hack to help prevent menu being
				 // clipped by Windows/Linux/Solaris
				 // Taskbar.

	int xOffset = 0;
	if (bottomRightMenu.x > (screenBounds.x + sizeScreen.width))
	    xOffset = -sizeMenu.width;

	int yOffset = 0;
	if (bottomRightMenu.y > (screenBounds.y + sizeScreen.height))
	    yOffset = sizeScreen.height - bottomRightMenu.y;

	return new Point(x + xOffset, y + yOffset);
    }
}

return new Point(x, y); // ? that would mean that the top left point was
			// not on any screen.
   }
 
Example 3
Source File: ListView.java    From netbeans with Apache License 2.0 5 votes vote down vote up
void createPopup(int xpos, int ypos, boolean contextMenu) {
    if (manager == null) {
        return;
    }

    if (!popupAllowed) {
        return;
    }
    
    if (!isShowing()) {
        return;
    }

    JPopupMenu popup;

    if (contextMenu) {
        popup = Utilities.actionsToPopup(manager.getExploredContext().getActions(true), this);
    } else {
        Action[] actions = NodeOp.findActions(manager.getSelectedNodes());
        popup = Utilities.actionsToPopup(actions, this);
    }

    if ((popup != null) && (popup.getSubElements().length > 0)) {
        Point p = getViewport().getViewPosition();
        p.x = xpos - p.x;
        p.y = ypos - p.y;

        SwingUtilities.convertPointToScreen(p, ListView.this);

        Dimension popupSize = popup.getPreferredSize();
        Rectangle screenBounds = Utilities.getUsableScreenBounds(getGraphicsConfiguration());

        if ((p.x + popupSize.width) > (screenBounds.x + screenBounds.width)) {
            p.x = (screenBounds.x + screenBounds.width) - popupSize.width;
        }

        if ((p.y + popupSize.height) > (screenBounds.y + screenBounds.height)) {
            p.y = (screenBounds.y + screenBounds.height) - popupSize.height;
        }

        SwingUtilities.convertPointFromScreen(p, ListView.this);
        popup.show(this, p.x, p.y);
    }
}
 
Example 4
Source File: JPopupMenuUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static Point getPopupMenuOrigin(JPopupMenu popup, Point p) {
    Point newPt = new Point(p);
    Dimension popupSize = popup.getPreferredSize();
    Rectangle screenRect = getScreenRect();
    int popupRight = newPt.x + popupSize.width;
    int popupBottom = newPt.y + popupSize.height;
    int screenRight = screenRect.x + screenRect.width;
    int screenBottom = screenRect.y + screenRect.height;

    if (popupRight > screenRight) { // Are we off the right edge?
        newPt.x = screenRight - popupSize.width;
    }

    if (newPt.x < screenRect.x) { // Are we off the left edge?
        newPt.x = screenRect.x;
    }

    if (popupBottom > screenBottom) { // Are we off the bottom edge?
        newPt.y = screenBottom - popupSize.height;
    }

    if (newPt.y < screenRect.y) { // Are we off the top edge?
        newPt.y = screenRect.y;
    }

    return newPt;
}
 
Example 5
Source File: DropdownButton.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void displayPopup() {
    JPopupMenu menu = new JPopupMenu();
    populatePopup(menu);
    if (menu.getComponentCount() > 0) {
        Dimension size = menu.getPreferredSize();
        size.width = Math.max(size.width, getWidth());
        menu.setPreferredSize(size);
        menu.show(this, 0, getHeight());
    }
}
 
Example 6
Source File: ProfilerTable.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void showPopupMenu(MouseEvent e) {
    JPopupMenu popup = new JPopupMenu() {
        public void setVisible(boolean visible) {
            if (visible) popupShowing();
            super.setVisible(visible);
            if (!visible) popupHidden();
        }
    };
    
    int row = getSelectedRow();
    Object value = getValueForRow(row);
    Object userValue = getUserValueForRow(row);
    populatePopup(popup, value, userValue);
    
    if (popup.getComponentCount() > 0) {
        if (e == null) {
            boolean b = row == -1;
            int c = b ? -1 : convertColumnIndexToView(mainColumn);
            Rectangle t = b ? getVisibleRect() : getCellRect(row, c, false);
            Dimension s = popup.getPreferredSize();
            int x = t.x + (t.width - s.width) / 2;
            int y = t.y + (b ? (t.height - s.height) / 2 : getRowHeight() - 4);
            popup.show(this, Math.max(x, 0), Math.max(y, 0));
        } else {
            popup.show(this, e.getX(), e.getY());
        }
    }
}
 
Example 7
Source File: PopupButton.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void displayPopup() {
    JPopupMenu menu = new JPopupMenu();
    populatePopup(menu);
    if (menu.getComponentCount() > 0) {
        Dimension size = menu.getPreferredSize();
        size.width = Math.max(size.width, getWidth());
        menu.setPreferredSize(size);
        
        int align = getPopupAlign();
        
        int x;
        switch (align) {
            case SwingConstants.EAST:
            case SwingConstants.NORTH_EAST:
            case SwingConstants.SOUTH_EAST:
                x = getWidth() - size.width;
                break;
            default:
                x = 0;
                break;
        }
        
        int y;
        switch (align) {
            case SwingConstants.NORTH:
            case SwingConstants.NORTH_EAST:
            case SwingConstants.NORTH_WEST:
                y = -size.height;
                break;
            default:
                y = getHeight();
                break;
        }
        
        menu.show(this, x, y);
    }
}
 
Example 8
Source File: MergeDialogComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Shows given popup on given coordinations and takes care about the
 *  situation when menu can exceed screen limits.
 *  Copied from org.netbeans.core.windows.frames.DefaultContainerImpl
 */
private static void showPopupMenu(JPopupMenu popup, Point p, Component comp) {
    SwingUtilities.convertPointToScreen(p, comp);
    Dimension popupSize = popup.getPreferredSize();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    
    if (p.x + popupSize.width > screenSize.width) {
        p.x = screenSize.width - popupSize.width;
    }
    if (p.y + popupSize.height > screenSize.height) {
        p.y = screenSize.height - popupSize.height;
    }
    SwingUtilities.convertPointFromScreen(p, comp);
    popup.show(comp, p.x, p.y);
}
 
Example 9
Source File: LinkButton.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private void showPopupMenu(MouseEvent e) {
    JPopupMenu popup = new JPopupMenu();
    populatePopup(popup);
    
    if (popup.getComponentCount() > 0) {
        Dimension pref = popup.getPreferredSize();
        if (e == null) {
            popup.show(this, getWidth() / 2, -pref.height);
        } else {
            popup.show(this, e.getX(), e.getY() - pref.height);
        }
    }
}
 
Example 10
Source File: LinkButton.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private void showPopupMenu(MouseEvent e) {
    JPopupMenu popup = new JPopupMenu();
    populatePopup(popup);
    
    if (popup.getComponentCount() > 0) {
        Dimension pref = popup.getPreferredSize();
        if (e == null) {
            popup.show(this, getWidth() / 2, -pref.height);
        } else {
            popup.show(this, e.getX(), e.getY() - pref.height);
        }
    }
}
 
Example 11
Source File: DropdownButton.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
public void displayPopup() {
    JPopupMenu menu = new JPopupMenu();
    populatePopup(menu);
    if (menu.getComponentCount() > 0) {
        Dimension size = menu.getPreferredSize();
        size.width = Math.max(size.width, getWidth());
        menu.setPreferredSize(size);
        menu.show(this, 0, getHeight());
    }
}
 
Example 12
Source File: ProfilerTable.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private void showPopupMenu(MouseEvent e) {
    JPopupMenu popup = new JPopupMenu() {
        public void setVisible(boolean visible) {
            if (visible) popupShowing();
            super.setVisible(visible);
            if (!visible) popupHidden();
        }
    };
    
    int row = getSelectedRow();
    Object value = getValueForRow(row);
    Object userValue = getUserValueForRow(row);
    populatePopup(popup, value, userValue);
    
    if (popup.getComponentCount() > 0) {
        if (e == null) {
            boolean b = row == -1;
            int c = b ? -1 : convertColumnIndexToView(mainColumn);
            Rectangle t = b ? getVisibleRect() : getCellRect(row, c, false);
            Dimension s = popup.getPreferredSize();
            int x = t.x + (t.width - s.width) / 2;
            int y = t.y + (b ? (t.height - s.height) / 2 : getRowHeight() - 4);
            popup.show(this, Math.max(x, 0), Math.max(y, 0));
        } else {
            popup.show(this, e.getX(), e.getY());
        }
    }
}
 
Example 13
Source File: PopupButton.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
protected void displayPopup() {
    JPopupMenu menu = new JPopupMenu();
    populatePopup(menu);
    if (menu.getComponentCount() > 0) {
        Dimension size = menu.getPreferredSize();
        size.width = Math.max(size.width, getWidth());
        menu.setPreferredSize(size);
        
        int align = getPopupAlign();
        
        int x;
        switch (align) {
            case SwingConstants.EAST:
            case SwingConstants.NORTH_EAST:
            case SwingConstants.SOUTH_EAST:
                x = getWidth() - size.width;
                break;
            default:
                x = 0;
                break;
        }
        
        int y;
        switch (align) {
            case SwingConstants.NORTH:
            case SwingConstants.NORTH_EAST:
            case SwingConstants.NORTH_WEST:
                y = -size.height;
                break;
            default:
                y = getHeight();
                break;
        }
        
        menu.show(this, x, y);
    }
}
 
Example 14
Source File: GraphicUtils.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
    * Returns a point where the given popup menu should be shown. The point is
    * calculated by adjusting the X and Y coordinates so that the popup menu
    * will not be clipped by the screen boundaries.
    * 
    * @param popup
    *            the popup menu
    * @param x
    *            the x position in screen coordinate
    * @param y
    *            the y position in screen coordinates
    * @return the point where the popup menu should be shown in screen
    *         coordinates
    */
   public static Point getPopupMenuShowPoint(JPopupMenu popup, int x, int y) {
Dimension sizeMenu = popup.getPreferredSize();
Point bottomRightMenu = new Point(x + sizeMenu.width, y
	+ sizeMenu.height);

Rectangle[] screensBounds = getScreenBounds();
int n = screensBounds.length;
for (int i = 0; i < n; i++) {
    Rectangle screenBounds = screensBounds[i];
    if (screenBounds.x <= x
	    && x <= (screenBounds.x + screenBounds.width)) {
	Dimension sizeScreen = screenBounds.getSize();
	sizeScreen.height -= 32; // Hack to help prevent menu being
				 // clipped by Windows/Linux/Solaris
				 // Taskbar.

	int xOffset = 0;
	if (bottomRightMenu.x > (screenBounds.x + sizeScreen.width))
	    xOffset = -sizeMenu.width;

	int yOffset = 0;
	if (bottomRightMenu.y > (screenBounds.y + sizeScreen.height))
	    yOffset = sizeScreen.height - bottomRightMenu.y;

	return new Point(x + xOffset, y + yOffset);
    }
}

return new Point(x, y); // ? that would mean that the top left point was
			// not on any screen.
   }
 
Example 15
Source File: JPopupMenuUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static void dynamicChangeToSubmenu(JPopupMenu popup, boolean usedToBeContained) {
    Object invoker = popup.getInvoker();

    if (!(invoker instanceof JMenu)) {
        return;
    }

    JMenu menu = (JMenu) invoker;

    if (!popup.isShowing()) {
        return;
    }

    if (isProblemConfig()) {
        callRefreshLater2(popup, menu);

        return;
    }

    refreshPopup(popup);

    Point p = popup.getLocationOnScreen();
    Dimension popupSize = popup.getPreferredSize();
    Rectangle popupRect = new Rectangle(p, popupSize);
    Rectangle screenRect = getScreenRect();
    boolean willBeContained = isPopupContained(popup);

    if (!screenRect.contains(popupRect)) {
        /*
         * The menu grew off the edge of the screen.
         */
        menu.setPopupMenuVisible(false);
        menu.setPopupMenuVisible(true);
    } else if (usedToBeContained != willBeContained) {
        /*
         * The menu grew off the edge of the containing window.
         * Use the setVisible() hack to change the menu from
         * lightweight to heavyweight.
         */
        popup.setVisible(false);
        popup.setVisible(true);
    }
}