Swing/TooltipInTableCell のバックアップソース(No.9)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/TooltipInTableCell へ行く。
- 1 (2012-12-05 (水) 18:23:06)
- 2 (2012-12-07 (金) 16:28:50)
- 3 (2013-10-24 (木) 15:07:54)
- 4 (2014-02-18 (火) 15:26:39)
- 5 (2015-02-10 (火) 15:32:04)
- 6 (2015-12-01 (火) 21:03:31)
- 7 (2017-04-07 (金) 13:51:51)
- 8 (2017-05-23 (火) 14:21:28)
- 9 (2018-02-24 (土) 19:51:30)
- 10 (2018-05-11 (金) 16:31:56)
- 11 (2020-05-03 (日) 14:12:45)
- 12 (2021-10-29 (金) 04:10:07)
--- category: swing folder: TooltipInTableCell title: JTableのセルに複数配置したコンポーネントのJToolTip tags: [JTable, JToolTip, ImageIcon] author: aterai pubdate: 2012-12-03T02:37:08+09:00 description: JTableのセル中に複数個配置したコンポーネントにそれぞれJToolTipが表示されるように設定します。 image: https://lh4.googleusercontent.com/-OhwzDU_Mys4/ULuQJmYCRiI/AAAAAAAABYQ/Y3Q5mVlliHs/s800/TooltipInTableCell.png hreflang: href: http://java-swing-tips.blogspot.com/2015/12/show-jtooltip-for-icons-placed-in-cell.html lang: en --- * 概要 [#summary] `JTable`のセル中に複数個配置したコンポーネントにそれぞれ`JToolTip`が表示されるように設定します。 #download(https://lh4.googleusercontent.com/-OhwzDU_Mys4/ULuQJmYCRiI/AAAAAAAABYQ/Y3Q5mVlliHs/s800/TooltipInTableCell.png) * サンプルコード [#sourcecode] #code(link){{ JTable table = new JTable(model) { @Override public String getToolTipText(MouseEvent e) { Point pt = e.getPoint(); int vrow = rowAtPoint(pt); int vcol = columnAtPoint(pt); int mcol = convertColumnIndexToModel(vcol); if (mcol == 1) { TableCellRenderer tcr = getCellRenderer(vrow, vcol); Component c = prepareRenderer(tcr, vrow, vcol); if (c instanceof JPanel) { Rectangle r = getCellRect(vrow, vcol, true); c.setBounds(r); c.doLayout(); pt.translate(-r.x, -r.y); Component l = SwingUtilities.getDeepestComponentAt(c, pt.x, pt.y); if (l instanceof JLabel) { ImageIcon icon = (ImageIcon) ((JLabel) l).getIcon(); return icon.getDescription(); } } } return super.getToolTipText(e); } }; }} * 解説 [#explanation] 上記のサンプルでは、`JTable#getToolTipText(MouseEvent)`をオーバーライドして、`JTable`のセルに複数配置したコンポーネントの情報を`ToolTipText`として返すように設定しています。 - `JTable#prepareRenderer(...)`メソッドで、現在マウスカーソルがあるセルのレンダラー(描画用コンポーネント)を取得 - 取得したコンポーネントの位置、サイズ、レイアウトを更新 -- 参考: [https://stackoverflow.com/questions/10854831/tool-tip-in-jpanel-in-jtable-not-working java - Tool tip in JPanel in JTable not working - Stack Overflow] - 更新したコンポーネント内から、`SwingUtilities.getDeepestComponentAt()`で、マウスカーソル直下のアイコン(`JLabel`)を取得 - `ImageIcon#getDescription()`で取得した文字列を`ToolTipText`として返す * 参考リンク [#reference] - [https://stackoverflow.com/questions/10854831/tool-tip-in-jpanel-in-jtable-not-working java - Tool tip in JPanel in JTable not working - Stack Overflow] - [[JTableのTooltipsを行ごとに変更>Swing/RowTooltips]] - [[JListのセル中に配置したコンポーネント毎にカーソルを変更する>Swing/CursorOfCellComponent]] * コメント [#comment] #comment #comment