There are essentially two things missing. First of all, JToolTip
extends JComponent
, and unlike JPanel
, it doesn't have a default layout. To stretch the content
across the tooltip, use a BorderLayout
.
setLayout(new BorderLayout());
The second problem is the size. The ToolTipManager
respects the preferred size of the tool tip. While the BorderLayout
calculates the size, the ToolTipUI
ignores it. So, there are two alternatives: Manually set the preferred size...
setPreferredSize(content.getPreferredSize());
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:
setComponent(StadtLabel.this);
JPanel images = new JPanel(null);
waren.setLayout(new BoxLayout(waren, BoxLayout.X_AXIS));
for(ImageIcon icon:myIcons) {
JLabel lbl = 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);
setLayout(new BorderLayout());
add(content);
setUI(new ToolTipUI() {
@Override
public Dimension getMinimumSize(JComponent c) {
return c.getLayout().minimumLayoutSize(c);
}
@Override
public Dimension getPreferredSize(JComponent c) {
return c.getLayout().preferredLayoutSize(c);
}
@Override
public Dimension getMaximumSize(JComponent c) {
return getPreferredSize(c);
}
});