Transparent JToolTip background outside frame in Linux

I’m working on a Java swing application (I’m using JDK 22) where I customize basically every GUI component.

The problem I’m facing is that I’ve made a custom tooltip UI class where I paint it as a rounded rectangle in order to meet overall program aesthethics:

public class CustomToolTipUI extends ToolTipUI {

  @SuppressWarnings("java:S1172")
  public static ComponentUI createUI(JComponent component) {
    return new CustomToolTipUI();
  }

  @Override
  public void paint(Graphics g, JComponent c) {
    Graphics2D g2 = (Graphics2D) g.create();

    RoundRectangle2D roundedRect = new RoundRectangle2D.Float(0, 0, c.getWidth(), c.getHeight(),
                                                              Constants.ROUNDED_BORDER_ARC_TOOLTIP,
                                                              Constants.ROUNDED_BORDER_ARC_TOOLTIP);

    // Round rectangle configuration
    g2.setRenderingHints(Constants.MAP_RENDERING_HINTS);
    g2.setComposite(AlphaComposite.Clear);
    g2.fill(roundedRect);

    // Background painting
    g2.setComposite(AlphaComposite.Src);
    g2.setColor(Constants.COLOR_GREEN_DARK_MEDIUM);
    g2.fill(roundedRect);

    // Text painting
    FontMetrics fm = g2.getFontMetrics();

    String text = ((JToolTip) c).getTipText();

    g2.setColor(Color.WHITE);
    g2.drawString(text, (c.getWidth() - fm.stringWidth(text)) / 2, (c.getHeight() - fm.getHeight()) / 2 + fm.getAscent());
    g2.dispose();
  }

  @Override
  public Dimension getPreferredSize(JComponent c) {
    FontMetrics fm = c.getFontMetrics(c.getFont());

    Insets insets = c.getInsets();

    return new Dimension(fm.stringWidth(((JToolTip) c).getTipText()) + insets.left + insets.right,
                         fm.getHeight() + insets.top + insets.bottom);
  }
}

I noticed that if the tooltip exceeds the frame borders, it is displayed inside a component, and that translates to having a regular white rectangle with a rounded rectangle painted inside of it:

That’s where I tried a lot of things and couldn’t fix it, until I came across this post!

I tried what it says, but first I had to create a custom tooltip class that uses my custom UI but overrides the addNotify method in order to achieve this:

public class CustomToolTip extends JToolTip {

  public CustomToolTip(JComponent component) {
    setOpaque(false);
    setComponent(component);
    setUI(new CustomToolTipUI());
  }

  @Override
  @SuppressWarnings("java:S1190")
  public void addNotify() {
    super.addNotify();

    setOpaque(false);

    Component parent = this.getParent();

    if (parent instanceof JComponent) {
      ((JComponent) parent).setOpaque(false);
    }

    try {
      Window window = SwingUtilities.windowForComponent(this);

      if (window != null) {
        window.setBackground(new Color(0, 0, 0, 0));
      }
    } catch (IllegalComponentStateException | UnsupportedOperationException _) {
      CommonFunctions.exitProgram(Error.ERROR_INTERNAL);
    }
  }

  @Override
  public Insets getInsets() {
    return Constants.INSETS_TOOLTIP;
  }
}

now i can override the createToolTip method in my custom label class like this:

@Override
public JToolTip createToolTip() {
  return new CustomToolTip(this);
}

and it works! here is an example:

Life’s good until I tried to run this in Linux to verify that my cross-platform compatibility is intact… but in Linux Mint 21.3 Cinnamon the white background shown in the first screenshot is still there…

I’ve tried A LOT of things (for example: toggling isOpaque, changing window opacity, changing color alpha, changing the window container shape…) but I can’t make it work in Linux and I’m running out of ideas. Plus, there’s almost no examples for making something like this on internet either so I can almost only rely on documentation, which I’ve read but I’m not sure of what could be the problem.

According to java.awt.Window.setBackground(Color) documentation:

All the following conditions must be met to enable the per-pixel transparency mode for this window:

- The PERPIXEL_TRANSLUCENT translucency must be supported by the graphics device where this window is located
- The window must be undecorated (see Frame.setUndecorated and Dialog.setUndecorated)
- The window must not be in full-screen mode (see GraphicsDevice.setFullScreenWindow(Window))

If any of those conditions are not met, the alpha value will be ignored (no transparency). I’ve check those values and everything’s ok. Transparency is supported on my Linux system.

What can be done here, or what can I try to solve this? Thank you very much in advance!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật