List of usage examples for javax.swing JToolTip setTipText
@BeanProperty(preferred = true, description = "Sets the text of the tooltip") public void setTipText(String tipText)
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.java2 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.atlassian.theplugin.idea.bamboo.tree.BuildTreeNode.java
public static void addTooltipToPanel(BambooBuildAdapter build, JPanel p) { final JToolTip jToolTip = p.createToolTip(); jToolTip.setTipText(buildTolltip(build, 0)); final int prefWidth = jToolTip.getPreferredSize().width; int width = prefWidth > MAX_TOOLTIP_WIDTH ? MAX_TOOLTIP_WIDTH : 0; p.setToolTipText(buildTolltip(build, width)); }
From source file:com.github.alexfalappa.nbspringboot.cfgprops.completion.CfgPropCompletionItem.java
@Override public CompletionTask createToolTipTask() { return new AsyncCompletionTask(new AsyncCompletionQuery() { @Override//from w ww . j a va 2s. c o m protected void query(CompletionResultSet completionResultSet, Document document, int i) { JToolTip toolTip = new JToolTip(); toolTip.setTipText("Press Enter to insert \"" + getText() + "\""); completionResultSet.setToolTip(toolTip); completionResultSet.finish(); } }); }
From source file:com.haulmont.cuba.desktop.sys.DesktopToolTipManager.java
protected void showTooltip(JComponent field, String text) { if (!field.isShowing()) return;//from w ww .j a va2 s .c o m if (StringUtils.isEmpty(text)) { return; } PointerInfo pointerInfo = MouseInfo.getPointerInfo(); if (pointerInfo == null) { return; } if (toolTipWindow != null) { hideTooltip(); } component = field; final JToolTip toolTip = new CubaToolTip(); toolTip.setTipText(text); // Location to display tooltip Point location = getToolTipLocation(pointerInfo, toolTip.getTipText()); final Popup tooltipContainer = PopupFactory.getSharedInstance().getPopup(field, toolTip, location.x, location.y); tooltipContainer.show(); window = tooltipContainer; toolTipWindow = toolTip; tooltipShowing = true; if (!(field instanceof ToolTipButton)) { toolTip.addMouseListener(this); field.addMouseListener(this); } }