• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTableで文字列をクリックした場合だけセルを選択状態にする
#navi(../)
RIGHT:Posted by [[terai]] at 2006-12-25
*JTableで文字列をクリックした場合だけセルを選択状態にする [#pd0f30f5]
JTableの文字列以外の場所がクリックされた場合、そのセルが選択されないようにします。

-&jnlp;
-&jar;
-&zip;

#screenshot

**サンプルコード [#o4125fda]
#code{{
private static final int ICON_SIZE = 16;
private static Rectangle getCellRect2(JTable table, Point pt, int row, int col) {
  FontMetrics fm = table.getFontMetrics(table.getFont());
  Object o = table.getValueAt(row, col);
  int w = fm.stringWidth(o.toString()) + ICON_SIZE + 2 + 2;
  Rectangle rect = table.getCellRect(row, col, true);
  rect.setSize(w, rect.height);
  return rect;
//SwingUtilities2.pointOutsidePrefSize(...)
private static Rectangle getCellRect2(JTable table, int row, int col) {
  TableCellRenderer tcr = table.getCellRenderer(row, col);
  Object value = table.getValueAt(row, col);
  Component cell = tcr.getTableCellRendererComponent(table, value, false, false, row, col);
  Dimension itemSize = cell.getPreferredSize();
  Rectangle cellBounds = table.getCellRect(row, col, false);
  cellBounds.width = itemSize.width;
  return cellBounds;
//   FontMetrics fm = table.getFontMetrics(table.getFont());
//   Object o = table.getValueAt(row, col);
//   int w = fm.stringWidth(o.toString()) + 16 + 2 + 2;
//   Rectangle rect = table.getCellRect(row, col, true);
//   rect.setSize(w, rect.height);
//   return rect;
}

class TestRenderer extends Box implements TableCellRenderer {
  private final MyLabel textLabel;
  private final JLabel  iconLabel = new JLabel();
  public int getStringWidth(int row, int column) {
    FontMetrics fm = table.getFontMetrics(table.getFont());
    Object o = table.getValueAt(row, column);
    return fm.stringWidth(o.toString())
           + iconLabel.getPreferredSize().width
           + textLabel.getInsets().left
           + textLabel.getInsets().right;
  }
//......
}}

**解説 [#hb039983]
上記のサンプルでは、Nameカラムのセル中にあるアイコンと文字列の上でクリックされた場合のみ、そのセルが選択されるようになっています。

- %%JTable#columnAtPoint(Point)メソッドをオーバーライドし MouseAdapter%% %%MouseInputAdapterを設定し、Nameカラムのセルの文字列上でクリックされたかどうかを判別%%
- %%クリックされたポイントがそのセルの文字列上に無い場合、別のセル(幅0のダミーカラム)がクリックされたように偽装し、現在の選択状態を解除%%
- JTable.putClientProperty("Table.isFileList", Boolean.TRUE) で、0列目の文字列以外がクリックされても選択されないように変更
-- Windows LnF のみ?
- 範囲選択の場合は、文字列の幅を自前で計算
- %%範囲選択の場合は、文字列の幅を自前で計算%%
//- 0列目のセル以外は、選択されても見た目は変化しないようにレンダラーで設定

**参考リンク [#s19c43e1]
-[[JTableのセルをエクスプローラ風に表示する>Swing/ExplorerLikeTable]]

**コメント [#sf7f8963]
- JTable#columnAtPoint(Point)メソッドをオーバーライドする方法では、CPUが100%になってしまうので、クリック、ドラッグした場合だけ、評価するように修正しました。 -- [[terai]] &new{2007-04-16 (月) 18:46:39};
- JTable#getToolTipText(MouseEvent)メソッドをオーバーライドして、Nameカラムのセルの余白では、ツールチップも表示しないように変更しました。 -- [[terai]] &new{2007-04-16 (月) 18:49:02};
- table.putClientProperty("Table.isFileList", Boolean.TRUE); を使えば、MouseListenerなどもすべて必要なさそう…。 -- [[terai]] &new{2010-01-01 (金) 02:02:50};
-- putClientProperty("Table.isFileList", Boolean.TRUE) を使用するように変更、[[JListのアイテムを範囲指定で選択>Swing/RubberBanding]]での範囲選択機能を追加。 -- [[terai]] &new{2010-01-05 (火) 16:07:48};

#comment