1

I've created this custom JToolTip for my application. When the tooltip is entirely diplayed inside a JFrame, no background is visible (expected), but when the tooltip is displayed outside the JFrame, the background will be visible. How can I have it removed either way?

I've tried setBackground(new Color(255, 255, 255, 0)); with the '0' alpha value to make sure the background is transparent, but that didn't do the trick.

The tooltip inside the frame, as expected:

tooltip inside frame

The tooltip exceeding the JFrame, with the unwanted background:

tooltip exceeding frame

The custom JTooltip:

 public class DefaultToolTip extends JToolTip {

        public DefaultToolTip() {
            setOpaque(false);
            setPreferredSize(new Dimension(275, 30));
            setBackground(new Color(255, 255, 255, 0));
        }

        @Override
        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);
                }
            }
        }

        @Override
        public void paint(Graphics g) {
            String text = getComponent().getToolTipText();

            addNotify();
            Graphics2D g2d = drawComponent(g);
            drawText(text, g2d);

            g2d.dispose();
        }

        private void drawText(String text, Graphics2D g2d) {
            //Draw the text
            int cHeight = getComponent().getHeight();
            FontMetrics fm = g2d.getFontMetrics();
            g2d.setColor(Color.WHITE);
            if (cHeight > getHeight())
                g2d.drawString(text, (getWidth() - fm.stringWidth(text)) / 2, (getHeight() + fm.getAscent()) / 2 + 2);
            else
                g2d.drawString(text, (getWidth() - fm.stringWidth(text)) / 2, (cHeight + fm.getAscent()) / 2 + 2);
        }

        private Graphics2D drawComponent(Graphics g) {
            //Create a round rectangle
            Shape round = new RoundRectangle2D.Float(0, 8, getWidth(), getHeight(), 8, 8);

            //Draw the background
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setColor(Color.GRAY);
            g2d.fill(round);

            //Draw the left triangle
            Point p1 = new Point(getWidth() / 2, getHeight() - 30);
            Point p2 = new Point(getWidth() / 2 + 8, getHeight() - 20);
            Point p3 = new Point(getWidth() / 2 - 8, getHeight() - 20);
            int[] xs = {p1.x, p2.x, p3.x};
            int[] ys = {p1.y, p2.y, p3.y};
            Polygon triangle = new Polygon(xs, ys, xs.length);
            g2d.fillPolygon(triangle);
            return g2d;
        }
    }

Solution

A few things have changed to have the tooltip behave as expected. the paint method has been replaced by the paintComponent method, the addNotify call was removed, the method updated to fetch the window of the component and to give it a transparent background. setBorder(BorderFactory.createEmptyBorder()); was also needed to remove the components default border.

public class DefaultToolTip extends JToolTip {

    public DefaultToolTip() {
        setOpaque(false);
        setPreferredSize(new Dimension(275, 30));
        setBackground(new Color(255, 255, 255, 0));
        setBorder(BorderFactory.createEmptyBorder());
    }

    @Override
    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);
            }
        }
        Window window = SwingUtilities.windowForComponent(this);
        try {
            window.setBackground(new Color(255, 255, 255, 0));
        } catch (IllegalComponentStateException e) {
            //Do nothing
        }
    }

    @Override
    public void paintComponent(Graphics g) {
        //super.paintComponent(g);
        String text = getComponent().getToolTipText();

        Graphics2D g2d = drawComponent(g);
        drawText(text, g2d);

        g2d.dispose();
    }

    private void drawText(String text, Graphics2D g2d) {
        //Draw the text
        int cHeight = getComponent().getHeight();
        FontMetrics fm = g2d.getFontMetrics();
        g2d.setColor(Color.WHITE);
        if (cHeight > getHeight())
            g2d.drawString(text, (getWidth() - fm.stringWidth(text)) / 2, (getHeight() + fm.getAscent()) / 2 + 2);
        else
            g2d.drawString(text, (getWidth() - fm.stringWidth(text)) / 2, (cHeight + fm.getAscent()) / 2 + 2);
    }

    private Graphics2D drawComponent(Graphics g) {
        //Create a round rectangle
        Shape round = new RoundRectangle2D.Float(0, 8, getWidth(), getHeight(), 8, 8);

        //Draw the background
        Graphics2D g2d = (Graphics2D) g.create();
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.GRAY);
        g2d.fill(round);

        //Draw the left triangle
        Point p1 = new Point(getWidth() / 2, getHeight() - 30);
        Point p2 = new Point(getWidth() / 2 + 8, getHeight() - 20);
        Point p3 = new Point(getWidth() / 2 - 8, getHeight() - 20);
        int[] xs = {p1.x, p2.x, p3.x};
        int[] ys = {p1.y, p2.y, p3.y};
        Polygon triangle = new Polygon(xs, ys, xs.length);
        g2d.fillPolygon(triangle);
        return g2d;
    }
}

A note however, super.paintComponent(g) was commented out, since it would draw text another time.

1 Answer 1

2

Don't know if any of these will help but:

  1. Don't override paint(...). Custom painting is done by overriding paintComponent(...).

  2. Invoke super.paintComponent(...) as the first statement

  3. Don't invoke addNotify() in a painting method. A painting method is for painting only.

with the '0' alpha value to make sure the background is transparent,

Swing components don't know how to handle transparent backgrounds. Just make the component non-opaque.

When the tooltip overlaps the component. The tooltip is actually added to a JWindow before it is displayed. So in your addNotify() logic, you can search for the window and make it transparent.

Check out:

Window window = SwingUtilities.windowForComponent(...);
1
  • Thank you for the outstanding answer and feedback! I will add the solution to the answer.
    – Thibstars
    Dec 6, 2016 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.