Silly Freak Silly Freak - 3 years ago 207
Java Question

Custom Java tool tip with Swing components as content does not show up

I'm trying to show multiple images in a component's tooltip, found

and implemented a custom that adds the needed components like this:


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);


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?

Answer Source

The base "problems" are that JToolTip

  • is-not designed as a container, it's only accidentally a container because JComponent is. For a Swing "not-container" its the responsibility of the ui-delegate to act as LayoutManager.
  • isn't rich enough, it can handle text-only (at least with the emergency door html, which is @Andrew's favourite :-)

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:

public Dimension getPreferredSize() {
    if (getLayout() != null) {
        return getLayout().preferredLayoutSize(this);
    return super.getPreferredSize();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download