#rounded borders in jtooltip

10 messages · Page 1 of 1 (latest)

finite remnant
#

hi! i'm trying to customize my program gui (using only java swing) and i'm currently dealing with tooltips. since everything in my program has rounded corners, i wanted the tooltips to have also rounded corners. i tried a lot of things but i can't make it work. the rounded rectangle is drawn perfectly but there are 4 white corners that i can't get rid of (as you can see in the attached picture) even setting the transparency with AlphaComposite.Clear and i'm running out of ideas

this is a custom dummy class i made to test things until i can find the solution:

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;

public class CustomTooltip extends JToolTip {

  private final Color backgroundColor;
  private final Color foregroundColor;

  public CustomTooltip(JComponent component, Color backgroundColor, Color foregroundColor) {
    super();
    setComponent(component);
    this.backgroundColor = backgroundColor;
    this.foregroundColor = foregroundColor;
    setOpaque(false);
    setBorder(new EmptyBorder(5, 10, 5, 10));
  }

  @Override
  protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // transparency
    g2d.setComposite(AlphaComposite.Clear);
    g2d.fillRect(0, 0, getWidth(), getHeight());
    g2d.setComposite(AlphaComposite.SrcOver);

    // rounded rectangle
    g2d.setColor(backgroundColor);
    g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 15, 15);

    // text
    g2d.setColor(foregroundColor);
    FontMetrics fm = g.getFontMetrics();
    int x = 10; // text position
    int y = fm.getAscent() + 5;
    g2d.drawString(getTipText(), x, y);

    g2d.dispose();
  }
}

any ideas or recommendations are very helpful! thank you in advance

tiny roostBOT
#

This post has been reserved for your question.

Hey @finite remnant! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

tiny roostBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

tiny roostBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

tiny roostBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping.
Warning: abusing this will result in moderative actions taken against you.

ember topaz
#

So you want to use Graphics2D to create a rectangle with rounded corners?

#

but ofc with both sides