I wonder why nobody suggested to use a Popup
for this.
This is basically what is used "under the hood" of tool tips (and popup menus). The main advantage here is that you do not have take care about the layout, and (in contrast to standard tool tips) have full control over when it appears and when it disappears. So you can explicitly create the popup when the icon is clicked, and explicitly hide it when the mouse exits the icon:

Here is the code as a MCVE :
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class PopupExample
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel(new BorderLayout());
p.add(new JLabel("Radius:"), BorderLayout.WEST);
p.add(new JTextField(10), BorderLayout.CENTER);
Icon icon = UIManager.getIcon("OptionPane.questionIcon");
JLabel label = new JLabel(icon);
addHelpPopup(label, "<html>"
+ "The help text. You can (but do <br>"
+ "not have to) use <i>HTML</i> here for <br>"
+ "<u>formatting</u>"
+ "</html>");
p.add(label, BorderLayout.EAST);
f.getContentPane().setLayout(new FlowLayout());
f.getContentPane().add(p);
f.add(label);
f.setSize(400, 300);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static void addHelpPopup(Component component, String text)
{
component.addMouseListener(new MouseAdapter()
{
private Popup popup;
@Override
public void mouseClicked(MouseEvent e)
{
if (popup != null)
{
popup.hide();
popup = null;
}
PopupFactory popupFactory = PopupFactory.getSharedInstance();
JLabel label = new JLabel(text);
label.setOpaque(true);
label.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.BLACK),
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
Dimension dim = label.getPreferredSize();
Point p = e.getLocationOnScreen();
popup = popupFactory.getPopup(
component, label, p.x, p.y - dim.height);
popup.show();
}
@Override
public void mouseExited(MouseEvent e)
{
if (popup != null)
{
popup.hide();
popup = null;
}
}
});
}
}