Code
JEditorPane hint = new JEditorPane();
hint.setEditorKit(new HTMLEditorKit());
hint.setEditable(false);
hint.setOpaque(false);
JCheckBox check = new JCheckBox();
check.setOpaque(false);
JPanel panel = new JPanel(new BorderLayout());
panel.add(hint);
panel.add(check, BorderLayout.EAST);
JPopupMenu popup = new JPopupMenu();
popup.add(new JScrollPane(panel));
popup.setBorder(BorderFactory.createEmptyBorder());
JEditorPane editor = new JEditorPane() {
@Override public JToolTip createToolTip() {
JToolTip tip = super.createToolTip();
tip.addHierarchyListener(e -> {
if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0
&& e.getComponent().isShowing()) {
panel.setBackground(tip.getBackground());
popup.show(tip, 0, 0);
}
});
return tip;
}
};
editor.setEditorKit(new HTMLEditorKit());
editor.setText(HTML_TEXT);
editor.setEditable(false);
editor.addHyperlinkListener(e -> {
JEditorPane editorPane = (JEditorPane) e.getSource();
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
JOptionPane.showMessageDialog(editorPane, "You click the link with the URL " + e.getURL());
} else if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
editorPane.setToolTipText("");
Optional.ofNullable(e.getSourceElement())
.map(elem -> (AttributeSet) elem.getAttributes().getAttribute(HTML.Tag.A))
.ifPresent(attr -> {
String title = Objects.toString(attr.getAttribute(HTML.Attribute.TITLE));
String url = Objects.toString(e.getURL());
// String url = Objects.toString(attr.getAttribute(HTML.Attribute.HREF));
hint.setText(String.format("%s: %s", title, url, url));
popup.pack();
});
} else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
editorPane.setToolTipText(null);
}
});
- Override
JComponent#createToolTip()method to addHierarchyListenertoJToolTip - Show
JPopupMenuwithJToolTipas parent whenJToolTipis visible JToolTiphides behindJPopupMenuJPopupMenuadds aJPanelwith aJEditorPaneand aJCheckBoxinstead of aJMenuItem- Hiding the parent
JToolTipwith the mouse cursor does not close theJPopupMenu, so you can click the internalJCheckBoxor select the text in theJEditorPaneand copy it with Ctrl-C, etc - It is a normal
JPopupMenu, so it is hidden when the focus is moved by clicking on the parentJFrameetc




