简体   繁体   中英

Java/Swing - Tooltip rectangle

I'm currently developing a java/swing project, and i'm stuck in the customization process.
I've extended JToolTip and overrided the paint() method to draw my own tooltip, but i can't manage to remove the background around the tooltip.

Here's the paint() override:

/* (non-Javadoc)
 * @see javax.swing.JComponent#paint(java.awt.Graphics)
 */
@Override
public void paint(Graphics g) {     
    String text = getComponent().getToolTipText();

    if (text != null && text.trim().length() > 0) {
        // set the parent to not be opaque
        Component parent = this.getParent();
        if (parent != null) {
            if (parent instanceof JComponent) {
                JComponent jparent = (JComponent) parent;
                if (jparent.isOpaque()) {
                    jparent.setOpaque(false);
                }
            }
        }

        // create a round rectangle
        Shape round = new RoundRectangle2D.Float(4, 4, this.getWidth() - 1 - 8, this.getHeight() - 1 - 8, 8, 8);

        // draw the background
        Graphics2D g2 = (Graphics2D) g.create();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(getBackground());
        g2.fill(round);

        // draw the left triangle
        Point p1 = new Point(4, 10);
        Point p2 = new Point(4, 20);
        Point p3 = new Point(0, 15);
        int[] xs = {p1.x, p2.x, p3.x};
        int[] ys = {p1.y, p2.y, p3.y};
        Polygon triangle = new Polygon(xs, ys, xs.length);
        g2.fillPolygon(triangle);

        // draw the text
        int cHeight = getComponent().getHeight();
        FontMetrics fm = g2.getFontMetrics();
        g2.setColor(getForeground());
        if (cHeight > getHeight())
            g2.drawString(text, 10, (getHeight() + fm.getAscent()) / 2);
        else
            g2.drawString(text, 10, (cHeight + fm.getAscent()) / 2);

        g2.dispose();
    }
}

Here's the (white) background i want to remove:
在此输入图像描述

I'm running java 1.7.0_05 under OSX.

It may be because you're setting the parent as opaque after calling super.paint . Also, have you called setOpaque on the JToolTip ? Finally, you can do everything in addNotify instead of executing the code at every draw:

public void addNotify() {
    super.addNotify();
    setOpaque(false);
    Component parent = this.getParent();
    if (parent != null) {
        if (parent instanceof JComponent) {
            JComponent jparent = (JComponent) parent;
            jparent.setOpaque(false);
        }
    }
}

There is the problem that all components have by default a rectangular shape - if you want to build your own shape for tooltips you have to set it:

Shape circle = new Ellipse2D.Float(-1.0f,-1.0f, 36.0f, 36.0f);
tooltip.setShape(circle);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM