/** * Displays the popup menu at the position x,y in the coordinate space of the component invoker. * * @param invoker the component in whose space the popup menu is to appear * @param x the x coordinate in invoker's coordinate space at which the popup menu is to be * displayed * @param y the y coordinate in invoker's coordinate space at which the popup menu is to be * displayed */ public void show(Component invoker, int x, int y) { if (DEBUG) { System.out.println("in JPopupMenu.show "); } setInvoker(invoker); Frame newFrame = getFrame(invoker); if (newFrame != frame) { // Use the invoker's frame so that events // are propagated properly if (newFrame != null) { this.frame = newFrame; if (popup != null) { setVisible(false); } } } Point invokerOrigin; if (invoker != null) { invokerOrigin = invoker.getLocationOnScreen(); // To avoid integer overflow long lx, ly; lx = ((long) invokerOrigin.x) + ((long) x); ly = ((long) invokerOrigin.y) + ((long) y); if (lx > Integer.MAX_VALUE) lx = Integer.MAX_VALUE; if (lx < Integer.MIN_VALUE) lx = Integer.MIN_VALUE; if (ly > Integer.MAX_VALUE) ly = Integer.MAX_VALUE; if (ly < Integer.MIN_VALUE) ly = Integer.MIN_VALUE; setLocation((int) lx, (int) ly); } else { setLocation(x, y); } setVisible(true); }
public static void setPositionRelativeToParent( Window w, Component parent, int hOffset, int vOffset) { try { Point p = parent.getLocationOnScreen(); int x = (int) p.getX() + hOffset; int y = (int) p.getY() + vOffset; w.setLocation(x, y); w.pack(); } catch (Throwable t) { } }
@Override public Dimension getPreferredSize() { final Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); Point p = null; if (focusOwner != null && focusOwner.isShowing()) { p = focusOwner.getLocationOnScreen(); } return computeNotBiggerDimension(super.getPreferredSize().getSize(), p); }
public static Point getRelLocation(Component c) { if (c == null || !c.isShowing()) { return new Point(0, 0); } Container parent = getRootContainer(c); if ((parent != null) && parent.isShowing()) { Point p1 = c.getLocationOnScreen(); Point p2 = parent.getLocationOnScreen(); return new Point(p1.x - p2.x, p1.y - p2.y); } return new Point(0, 0); }
protected void customizePopup(BasicComboPopup popup) { scrollPane = getScrollPane(popup); if (popupWider) popupWider(popup); checkHorizontalScrollBar(popup); // For some reason in JDK7 the popup will not display at its preferred // width unless its location has been changed from its default // (ie. for normal "pop down" shift the popup and reset) Component comboBox = popup.getInvoker(); Point location = comboBox.getLocationOnScreen(); if (popupAbove) { int height = popup.getPreferredSize().height; popup.setLocation(location.x, location.y - height); } else { int height = comboBox.getPreferredSize().height; popup.setLocation(location.x, location.y + height - 1); popup.setLocation(location.x, location.y + height); } }
void doTest() throws Exception { ArrayList<Component> components = new ArrayList(); components.add(button); components.add(buttonLW); components.add(textField); components.add(textArea); components.add(list); components.add(listLW); int keys[]; String OS = System.getProperty("os.name").toLowerCase(); System.out.println(OS); if (OS.contains("os x") || OS.contains("sunos")) { keys = new int[] {KeyEvent.VK_SHIFT, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_META}; } else { keys = new int[] {KeyEvent.VK_SHIFT, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT}; } for (Component c : components) { System.out.print(c.getClass().getName() + ": "); Point origin = c.getLocationOnScreen(); int xc = origin.x + c.getWidth() / 2; int yc = origin.y + c.getHeight() / 2; Point center = new Point(xc, yc); robot.delay(robotDelay); robot.glide(origin, center); robot.click(); robot.delay(robotDelay); for (int k = 0; k < keys.length; ++k) { keyPressReceived = false; keyCode = keys[k]; robot.type(keyCode); robot.delay(robotDelay); if (!keyPressReceived) { synchronized (lock) { try { lock.wait(waitDelay); } catch (InterruptedException e) { } } } assertTrue(keyPressReceived, "key press event was not received"); } System.out.println("passed"); } robot.waitForIdle(); frame.dispose(); }