Swing/ToolTipAfterMouseWheelEvent のバックアップソース(No.1)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/ToolTipAfterMouseWheelEvent へ行く。
- 1 (2022-01-24 (月) 09:36:47)
- 2 (2022-01-25 (火) 09:23:38)
- 3 (2022-01-31 (月) 12:16:02)
- 4 (2022-02-01 (火) 22:31:49)
--- category: swing folder: ToolTipAfterMouseWheelEvent title: JListのセルのToolTipをMouseWheelEventに対応した表示位置に修正 tags: [JToolTip, JList, JScrollPane, MouseWheelListener, ToolTipManager] author: aterai pubdate: 2022-01-24T09:33:09+09:00 description: JScrollPane内のJListをMouseWheelで移動した後に表示されるToolTipの対象セルとその位置に修正します。 image: https://drive.google.com/uc?id=1wqY3utjMezRBlJ9j8WIest2ResTWWTMF --- * 概要 [#summary] JScrollPane内のJListをMouseWheelで移動した後に表示されるToolTipの対象セルとその位置に修正します。 #download(https://drive.google.com/uc?id=1wqY3utjMezRBlJ9j8WIest2ResTWWTMF) * サンプルコード [#sourcecode] #code(link){{ class TooltipList<E> extends JList<E> { protected TooltipList(ListModel<E> m) { super(m); } @Override public String getToolTipText(MouseEvent e) { Point p0 = e.getPoint(); Point p1 = getMousePosition(); if (p1 != null && !p1.equals(p0)) { int i = locationToIndex(p1); Rectangle cellBounds = getCellBounds(i, i); if (i >= 0 && cellBounds != null && cellBounds.contains(p1.x, p1.y)) { MouseEvent event = new MouseEvent( e.getComponent(), MouseEvent.MOUSE_MOVED, e.getWhen(), e.getModifiers(), p1.x, p1.y, e.getClickCount(), e.isPopupTrigger() ); return super.getToolTipText(event);; } } return super.getToolTipText(e); } @Override public Point getToolTipLocation(MouseEvent e) { Point p0 = e.getPoint(); Point p1 = getMousePosition(); if (p1 != null && !p1.equals(p0)) { int i = locationToIndex(p1); Rectangle cellBounds = getCellBounds(i, i); if (i >= 0 && cellBounds != null && cellBounds.contains(p1.x, p1.y)) { return new Point(p1.x, p1.y + cellBounds.height); } } return null; } } }} * 解説 [#explanation] - `Default` -- たとえば先頭の`0: 11111`セルにマウスカーソルを移動した直後にすばやくマウスホイールを下方向に回転して別セル上にマウスカーソルが配置された状態でもツールチップ表示される内容は`0: 11111`で、その表示位置も現在のマウスカーソル付近ではなく`JScrollPane`で隠されている`0: 11111`セルの位置になる -- マウスホイール対応以前の実装なのでマウス移動イベント以外でカーソル直下のセルが変化することは想定されていない -- `JTable`、`JTree`などでも同様 - `MouseWheelListener` -- 親`JScrollPane`に`MouseWheelListener`を追加し、ホイールイベントを`SwingUtilities.convertMouseEvent(...)`で`JList`ソースのマウスイベントに変換して`ToolTipManager.sharedInstance().mouseMoved(...)`で実行 -- `ToolTipManager`では`MouseEvent`の`ID`は考慮せずに位置のみ使用しているのでイベントソースの変更のみで十分 -- [Tooltips, the mouse wheel and JScrollPane ? oracle-tech https://community.oracle.com/tech/developers/discussion/1353509/tooltips-the-mouse-wheel-and-jscrollpane] - `getToolTipLocation` -- `JList#getToolTipText()`と`JList#getToolTipLocation()`をオーバーライドしてそれぞれ`MouseEvent#getPoint()`と`Component#getMousePosition()`で取得したマウス位置が異なる場合は`Component#getMousePosition()`で取得したマウス位置で`MouseEvent`を作成して実行 * 参考リンク [#reference] - [Tooltips, the mouse wheel and JScrollPane ? oracle-tech https://community.oracle.com/tech/developers/discussion/1353509/tooltips-the-mouse-wheel-and-jscrollpane] - [[JListのセル上にToolTipを表示する>Swing/ToolTipOnCellBounds]] * コメント [#comment] #comment #comment