List of usage examples for javax.swing JComponent getLocationOnScreen
public Point getLocationOnScreen()
From source file:Main.java
public static void openDialogNextToParent(final JComponent parentComponent, final JDialog dialog) { SwingUtilities.invokeLater(new Runnable() { @Override// w w w . j a v a2 s . co m public void run() { if (parentComponent != null) { Point parentLocation = parentComponent.getLocationOnScreen(); parentLocation.x += parentComponent.getWidth(); dialog.setLocation(parentLocation); } dialog.setVisible(true); dialog.setSize(dialog.getPreferredSize()); } }); }
From source file:Main.java
/** * Returns an appropriate location for a component's tool tip that <i>always</i> * lies within the specified frame.//w w w . j a va2 s .c o m * <p> * Intended be used in custom implementations of {@link JComponent#getToolTipLocation(MouseEvent)}. * * @param e * the event that caused the display of the tool tip * @param c * the parent component of the tool tip * @param frame * a component in which the tool tip has to fit (usually the surrounding window of "c") * @return */ public static Point getAdjustedToolTipLocation(MouseEvent e, JComponent c, Component frame) { JToolTip tip = new JToolTip(); tip.setTipText(c.getToolTipText(e)); Dimension tipSize = tip.getPreferredSize(); // Tool tip will be positioned within the bounds of the specified component (+ 5px inset) Rectangle frameR = frame.getBounds(); if (frame instanceof Container) { Container container = (Container) frame; Insets insets = container.getInsets(); frameR.x += insets.left; frameR.y += insets.top; frameR.width -= (insets.left + insets.right); frameR.height -= (insets.top + insets.bottom); } frameR.x += 5; frameR.y += 5; frameR.width -= 10; frameR.height -= 10; // Initial try for the tool tip's position Rectangle r = new Rectangle(e.getXOnScreen(), c.getLocationOnScreen().y + c.getSize().height + 1, tipSize.width, tipSize.height); // Check if it fits within the frame Rectangle intersection = frameR.intersection(r); if (r.equals(intersection)) { // Tool tip is fully visible within the frame --> use default behaviour // // Note: The implementation of ToolTipManager.showTipWindow() is not always // correct in dual screen mode. The tool tip is _always_ put on that screen, // where the most part of the frame lies upon, even if we return coordinates // that clearly belong to the other screen. Unfortunately we cannot change // that behavior... (bsh 2010-11-24) return null; } // Otherwise, move the tool tip int correction = 0; if (r.height == intersection.height) { // Height is okay, just move left. To make it look better, position the // tip 5px below the component. r = new Rectangle(r.x, c.getLocationOnScreen().y + c.getSize().height + 5, tipSize.width, tipSize.height); correction = -5; // needed to make the ToolTipManager use a lightweight pop-up } else { // The height does not fit. Position the tool tip above the component. r = new Rectangle(c.getLocationOnScreen().x + 10, c.getLocationOnScreen().y - tipSize.height - 1, tipSize.width, tipSize.height); } // Adjust to frame bounds intersection = frameR.intersection(r); intersection.x -= (r.width - intersection.width); intersection.y -= (r.height - intersection.height); // Return value is expected to be relative to the component's position return new Point((-c.getLocationOnScreen().x) + intersection.x + correction, (-c.getLocationOnScreen().y) + intersection.y); }
From source file:com.croer.javaorange.diviner.SimpleOrangeDiviner.java
@Override public void show(JComponent jComponent) { this.jComponent = jComponent; System.out.println(configuration.getString(jComponent.getName())); Point location = jComponent.getLocationOnScreen(); Dimension size = jComponent.getSize(); Point xy = new Point(location.x, location.y + size.height); setLocation(xy);//from w w w . j a v a2 s . c om // EventQueue.invokeLater(new Runnable() { // // public void run() { // jTextField3.requestFocusInWindow(); // } // }); pack(); setVisible(true); }
From source file:com.haulmont.cuba.desktop.sys.DesktopToolTipManager.java
protected boolean isPointInComponent(Point point, JComponent component) { if (!component.isShowing()) return false; Point componentLocation = component.getLocationOnScreen(); Rectangle bounds = component.getBounds(); return (((point.x >= componentLocation.x) && (point.x <= componentLocation.x + bounds.width)) && (point.y >= componentLocation.y) && (point.y <= componentLocation.y + bounds.height)); }