Swing/ToolTipLocation のバックアップソース(No.6)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/ToolTipLocation へ行く。
--- title: JToolTipの表示位置 tags: [JToolTip, JWindow, MouseListener, MouseMotionListener] author: aterai pubdate: 2010-05-03T22:32:22+09:00 description: JToolTipの表示位置がドラッグでマウスカーソルに追従するように設定します。 --- * 概要 [#e77cc7bb] `JToolTip`の表示位置がドラッグでマウスカーソルに追従するように設定します。 #download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTVoUwnbfI/AAAAAAAAAn8/lAHqv08RJKA/s800/ToolTipLocation.png) * サンプルコード [#p9c48ec0] #code(link){{ private void updateTipText(MouseEvent e) { Point pt = e.getPoint(); String txt = String.format("Window(x, y)=(%d, %d)", pt.x, pt.y); tip.setTipText(txt); Point p = getToolTipLocation(e); if (SwingUtilities.isLeftMouseButton(e)) { if (prev.length() != txt.length()) { window.pack(); } window.setLocation(p); window.setAlwaysOnTop(true); } else { if (popup != null) { popup.hide(); } popup = factory.getPopup(e.getComponent(), tip, p.x, p.y); Container c = tip.getTopLevelAncestor(); if (c instanceof JWindow && ((JWindow) c).getType() == Window.Type.POPUP) { System.out.println("Popup$HeavyWeightWindow"); } else { popup.show(); } } prev = txt; } }} * 解説 [#w32ecab3] - 左クリックしてドラッグ -- `JWindow`に、`JToolTip`を追加して、`Window#setLocation()`で移動 -- テキスト文字数が変更された場合のみ、`JWindow#pack()`を呼び出してサイズを更新 - 左クリック以外でドラッグ -- `PopupFactory#getPopup()`で座標を指定した、`Popup`を取得し表示 -- `Popup`の位置が変更できずこれを再作成しているため、親フレームの外に`JToolTip`が表示される場合(`HeavyWeightな`JWindow`を再作成すると表示がチラつく)は非表示にしている * 参考リンク [#i0350c1f] - [[JSliderのノブをドラッグ中にToolTipで値を表示>Swing/SliderToolTips]] * コメント [#s6271bc0] #comment #comment