TITLE:JTableのセルのハイライト

JTableのセルのハイライト

編集者:Terai Atsuhiro
作成日:2004-03-21
更新日:2021-11-18 (木) 11:29:54

概要

セル上にマウスがある場合、その色を変更します。

#screenshot

サンプルコード

class TestRenderer extends DefaultTableCellRenderer
                   implements MouseListener, MouseMotionListener {
  private final JTable table;
  private int row = -1;
  private int col = -1;
  public TestRenderer(JTable table) {
    super();
    this.table = table;
    table.addMouseListener(this);
    table.addMouseMotionListener(this);
  }
  public Component getTableCellRendererComponent(JTable table, Object value,
                           boolean isSelected, boolean hasFocus,
                           int row, int column) {
    super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
                                        row, column);
    setHorizontalAlignment((value instanceof Number)?RIGHT:LEFT);
    if(this.row==row && this.col==column) {
      setBackground(Color.RED);
    }else{
      setBackground(isSelected?table.getSelectionBackground()
                              :table.getBackground());
    }
    return this;
  }
  public void mouseMoved(MouseEvent e) {
    Point pt = e.getPoint();
    row = table.rowAtPoint(pt);
    col = table.columnAtPoint(pt);
    if(row<0 || col<0) {
      row = -1;
      col = -1;
    }
    table.repaint();
  }
  public void mouseExited(MouseEvent e)  {
    row = -1;
    col = -1;
    table.repaint();
  }
  public void mouseDragged(MouseEvent e) {}
  public void mouseClicked(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {}
  public void mousePressed(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
}
  • &jnlp;
  • &jar;
  • &zip;

解説

MouseListener、MouseMotionListenerをimplementsしたセルレンダラーを作成し、マウスカーソルが乗っているセルの色を変更しています。

JTable#prepareRendererをオーバーライドする場合も、同様の方法が使用できます。

class MyTable extends JTable implements MouseListener, MouseMotionListener {
  private int row = -1;
  private int col = -1;
  public MyTable() {
    super();
    addMouseListener(this);
    addMouseMotionListener(this);
  }
  public MyTable(TableModel model) {
    super(model);
    addMouseListener(this);
    addMouseMotionListener(this);
  }
  public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
    Component c = super.prepareRenderer(renderer, row, column);
    if(this.row==row && this.col==column) {
      c.setBackground(Color.RED);
    }else if(isRowSelected(row)) {
      c.setBackground(getSelectionBackground());
    }else{
      c.setBackground(Color.WHITE);
    }
    return c;
  }
  public void mouseMoved(MouseEvent e) {
    Point pt = e.getPoint();
    row = rowAtPoint(pt);
    col = columnAtPoint(pt);
    if(row<0 || col<0) {
      row = -1;
      col = -1;
    }
    repaint();
  }
  public void mouseExited(MouseEvent e)  {
    row = -1;
    col = -1;
    repaint();
  }
  public void mouseDragged(MouseEvent e) {}
  public void mouseClicked(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {}
  public void mousePressed(MouseEvent e)  {}
  public void mouseReleased(MouseEvent e) {}
}

参考リンク

コメント