/** * Returns an point which has been adjusted to take into account of the desktop bounds, taskbar * and multi-monitor configuration. * * <p>This adustment may be cancelled by invoking the application with * -Djavax.swing.adjustPopupLocationToFit=false */ Point adjustPopupLocationToFitScreen(int xPosition, int yPosition) { Point popupLocation = new Point(xPosition, yPosition); if (popupPostionFixDisabled == true || GraphicsEnvironment.isHeadless()) { return popupLocation; } // Get screen bounds Rectangle scrBounds; GraphicsConfiguration gc = getCurrentGraphicsConfiguration(popupLocation); Toolkit toolkit = Toolkit.getDefaultToolkit(); if (gc != null) { // If we have GraphicsConfiguration use it to get screen bounds scrBounds = gc.getBounds(); } else { // If we don't have GraphicsConfiguration use primary screen scrBounds = new Rectangle(toolkit.getScreenSize()); } // Calculate the screen size that popup should fit Dimension popupSize = JPopupMenu.this.getPreferredSize(); long popupRightX = (long) popupLocation.x + (long) popupSize.width; long popupBottomY = (long) popupLocation.y + (long) popupSize.height; int scrWidth = scrBounds.width; int scrHeight = scrBounds.height; if (!canPopupOverlapTaskBar()) { // Insets include the task bar. Take them into account. Insets scrInsets = toolkit.getScreenInsets(gc); scrBounds.x += scrInsets.left; scrBounds.y += scrInsets.top; scrWidth -= scrInsets.left + scrInsets.right; scrHeight -= scrInsets.top + scrInsets.bottom; } int scrRightX = scrBounds.x + scrWidth; int scrBottomY = scrBounds.y + scrHeight; // Ensure that popup menu fits the screen if (popupRightX > (long) scrRightX) { popupLocation.x = scrRightX - popupSize.width; if (popupLocation.x < scrBounds.x) { popupLocation.x = scrBounds.x; } } if (popupBottomY > (long) scrBottomY) { popupLocation.y = scrBottomY - popupSize.height; if (popupLocation.y < scrBounds.y) { popupLocation.y = scrBounds.y; } } return popupLocation; }
static { /* ensure that the necessary native libraries are loaded */ Toolkit.loadLibraries(); if (!GraphicsEnvironment.isHeadless()) { initIDs(); } }
/** * Perform the specified Action on the object * * @param i zero-based index of actions * @return true if the the action was performed; else false. */ public boolean doAccessibleAction(int i) { if (i == 0) { // Simulate a button click Toolkit.getEventQueue() .postEvent( new ActionEvent( Button.this, ActionEvent.ACTION_PERFORMED, Button.this.getActionCommand())); return true; } else { return false; } }
/** * Perform the specified Action on the object * * @param i zero-based index of actions * @return true if the action was performed; otherwise false. */ public boolean doAccessibleAction(int i) { if (i == 0) { // Simulate a button click Toolkit.getEventQueue() .postEvent( new ActionEvent( MenuItem.this, ActionEvent.ACTION_PERFORMED, MenuItem.this.getActionCommand(), EventQueue.getMostRecentEventTime(), 0)); return true; } else { return false; } }
/* * The main goal of this method is to post an appropriate event * to the event queue when menu shortcut is pressed. However, * in subclasses this method may do more than just posting * an event. */ void doMenuEvent(long when, int modifiers) { Toolkit.getEventQueue() .postEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, getActionCommand(), when, modifiers)); }
/** * Creates the menu item's peer. The peer allows us to modify the appearance of the menu item * without changing its functionality. */ public void addNotify() { synchronized (getTreeLock()) { if (peer == null) peer = Toolkit.getDefaultToolkit().createMenuItem(this); } }