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!