JToolTipの文字列を選択・コピー可能にする
Total: 2903, Today: 2, Yesterday: 1
Posted by aterai at
Last-modified:
Summary
JToolTipの代わりにJPopupMenuを表示し、その内部に配置したコンポーネントのクリックや文字列の選択・コピーを可能にします。
Screenshot

Advertisement
Source Code Examples
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("<html>%s: <a href='%s'>%s</a>", title, url, url));
popup.pack();
});
} else if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
editorPane.setToolTipText(null);
}
});
View in GitHub: Java, KotlinDescription
JComponent#createToolTip()メソッドをオーバーライドしてJToolTipにHierarchyListenerを追加JToolTipが表示状態になったらそのJToolTipを親にしてJPopupMenuを表示JToolTipはJPopupMenuの背後に隠れているJPopupMenuにはJMenuItemではなくJEditorPaneとJCheckBoxを配置したJPanelを追加している
- マウスカーソルを移動して親の
JToolTipが非表示になってもJPopupMenuは閉じないので、内部のJCheckBoxをクリックしたりJEditorPaneの文字列を選択しCtrl+Cなどでコピー可能- 通常の
JPopupMenuなので親JFrameなどをクリックしてフォーカスが移動すると非表示になる
- 通常の