Swing/ClippedCellTooltips のバックアップ差分(No.8)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/ClippedCellTooltips へ行く。
- 1 (2009-10-12 (月) 17:37:22)
- 2 (2009-10-13 (火) 16:39:07)
- 3 (2011-03-13 (日) 21:44:49)
- 4 (2013-01-05 (土) 19:53:38)
- 5 (2013-10-17 (木) 14:57:55)
- 6 (2015-02-06 (金) 19:36:07)
- 7 (2015-03-12 (木) 15:08:59)
- 8 (2016-12-22 (木) 11:54:42)
- 9 (2016-12-22 (木) 19:16:41)
- 10 (2017-12-09 (土) 02:03:40)
- 11 (2019-08-07 (水) 20:21:19)
- 12 (2021-04-07 (水) 11:07:53)
- 13 (2023-03-23 (木) 21:44:23)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: ClippedCellTooltips title: JTableのセルがクリップされている場合のみJToolTipを表示 tags: [JTable, JTableHeader, TableCellRenderer, JToolTip] author: aterai pubdate: 2009-10-12T17:37:22+09:00 description: JTableのセルがクリップされている場合のみJToolTipを表示します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTJNQAyg-I/AAAAAAAAAUA/F6oQbiUShl4/s800/ClippedCellTooltips.png --- * 概要 [#kb1661a0] * 概要 [#summary] `JTable`のセルがクリップされている場合のみ`JToolTip`を表示します。 #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTJNQAyg-I/AAAAAAAAAUA/F6oQbiUShl4/s800/ClippedCellTooltips.png) * サンプルコード [#a310ddc6] * サンプルコード [#sourcecode] #code(link){{ class ToolTipHeaderRenderer implements TableCellRenderer { private final Icon icon = UIManager.getIcon("Table.ascendingSortIcon"); @Override public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected,boolean hasFocus,int row,int column) { Object value, boolean isSelected, boolean hasFocus, int row, int column) { TableCellRenderer renderer = table.getTableHeader().getDefaultRenderer(); JLabel l = (JLabel) renderer.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column); Insets i = l.getInsets(); Rectangle rect = table.getCellRect(row, column, false); rect.width -= i.left + i.right; RowSorter<? extends TableModel> sorter = table.getRowSorter(); if (sorter != null && !sorter.getSortKeys().isEmpty() && sorter.getSortKeys().get(0).getColumn() == column) { rect.width -= icon.getIconWidth() + 2; //XXX } FontMetrics fm = l.getFontMetrics(l.getFont()); String str = value.toString(); int cellTextWidth = fm.stringWidth(str); l.setToolTipText(cellTextWidth > rect.width ? str : null); return l; } } }} * 解説 [#rb49016f] * 解説 [#explanation] - ヘッダセル -- `TableCellRenderer`で、セルの幅と文字列の長さを比較して、`ToolTip`を設定 -- ソートアイコンと文字列の間の`gap`が不明? - セル -- `JTable#prepareRenderer`メソッドをオーバーライドし、セルの幅と文字列の長さを比較して、`ToolTip`を設定 #code{{ JTable table = new JTable(model) { @Override public Component prepareRenderer(TableCellRenderer tcr, int row, int column) { Component c = super.prepareRenderer(tcr, row, column); if (c instanceof JComponent) { JComponent l = (JComponent) c; Object o = getValueAt(row, column); Insets i = l.getInsets(); Rectangle rect = getCellRect(row, column, false); rect.width -= i.left + i.right; FontMetrics fm = l.getFontMetrics(l.getFont()); String str = o.toString(); int cellTextWidth = fm.stringWidth(str); l.setToolTipText(cellTextWidth > rect.width ? str : null); } return c; } }; }} * 参考リンク [#l13d8dfe] * 参考リンク [#reference] - [[JTableHeaderのTooltipsを列ごとに変更>Swing/HeaderTooltips]] - [[JTableのTooltipsを行ごとに変更>Swing/RowTooltips]] * コメント [#kb103118] * コメント [#comment] #comment #comment