I'm trying to show multiple images in a component's tooltip, found createToolTip()
and implemented a custom that adds the needed components like this:
setComponent(component);
JPanel images = new JPanel(null);
images.setLayout(new BoxLayout(images, BoxLayout.X_AXIS));
for(ImageIcon icon:myIcons) {
images.add(new JLabel(icon));
}
JPanel content = new JPanel(new BorderLayout());
content.add(new JLabel(title), BorderLayout.NORTH);
content.add(new JLabel(description));
content.add(images, BorderLayout.SOUTH);
add(content);
However, all I see is a little dot, indicating that the tool tip is shown, but somehow the size is ignored. What do I miss implementing a custom tooltip?
I'd suggest to using
JWindow
or un_decoratedJDialog
, as popup window (used by default forJCalendar
orJDatePicker
) rather thanJTooltip
, for nicer output to the GUI implements Translucent and Shaped WindowsInstead of reinventing the wheel try this: https://github.com/timmolderez/balloontip. You can put any content as JComponent.
The base "problems" are that JToolTip
By-passing those limitations basically is a driving that widget nearly over the edge. A clean solution would roll a new component .. On the other hand, the OP already found the screws to tweak. The only thingy that could be slightly improved is to neither call setXXSize, nor set a custom ui. Instead, make it behave like a container by overriding getXXSize() like:
It might sound silly but, have you tried setting bounds for
JPanel
?And you can try setting a gap between components in
BorderLayout
There are essentially two things missing. First of all,
JToolTip
extendsJComponent
, and unlikeJPanel
, it doesn't have a default layout. To stretch thecontent
across the tooltip, use aBorderLayout
.The second problem is the size. The
ToolTipManager
respects the preferred size of the tool tip. While theBorderLayout
calculates the size, theToolTipUI
ignores it. So, there are two alternatives: Manually set the preferred size...Note that this does not make the layout obsolete; otherwise, you get an empty tool tip with the right size.
... or subclass
ToolTipUI
to respect the layout, which is what I went with. The resulting code is:Tool tips can render HTML. If you can form URLs to the images (not practical if they are generated in memory but usually doable otherwise), it is an easy matter to write some HTML that will load the images, and use that HTML as the tool tip.
E.G.