Somewhat based on idea by mKorbel, I made a glass pane style overlay atop the component group. In my case, all the components only provide information, and do not need user interaction, so the glass pane does not bother dispatching events to them.
I used OverlayLayout
, as it was simplest and sufficient for my purposes. Applied to the code in the question, ToolTipTest
constructor would be changed to:
ToolTipTest() {
JFrame frame = new JFrame("Tool tip test");
frame.setLocationByPlatform(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Holds the component group and the glass pane
JComponent content = new JPanel();
content.setLayout(new OverlayLayout(content));
frame.add(content);
JComponent glassPane = new JComponent(){};
content.add(glassPane);
final JPanel bg = new JPanel(new BorderLayout());
content.add(bg);
bg.add(new JLabel("Tooltip shows here"), BorderLayout.NORTH);
JTextPane text = new JTextPane();
try {
text.getStyledDocument().insertString(0, "The same tooltip also shows here", null);
} catch (BadLocationException e) {
e.printStackTrace();
}
text.setEditable(false);
text.setFocusable(false);
bg.add(text);
glassPane.setToolTipText("A tooltip, that shows on both the label and the text");
JLabel other = new JLabel("This component has another tooltip");
other.setToolTipText("Another tooltip");
frame.add(other, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
Edit: Included a label that is outside the component group with the shared tool tip, to differentiate from the more typical use of glass panes.