Swing/CellHighlight のバックアップ差分(No.10)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/CellHighlight へ行く。
- 1 (2005-03-21 (月) 07:27:38)
- 2 (2005-03-21 (月) 10:24:46)
- 3 (2005-04-28 (木) 04:32:51)
- 4 (2005-09-22 (木) 21:56:11)
- 5 (2006-02-27 (月) 15:31:37)
- 6 (2006-04-12 (水) 19:36:06)
- 7 (2006-11-04 (土) 10:44:09)
- 8 (2007-03-28 (水) 18:36:41)
- 9 (2008-03-14 (金) 16:27:20)
- 10 (2008-03-14 (金) 21:37:45)
- 11 (2011-03-21 (月) 01:39:47)
- 12 (2013-02-20 (水) 13:38:39)
- 13 (2013-03-30 (土) 21:21:38)
- 14 (2014-11-16 (日) 04:48:09)
- 15 (2014-11-17 (月) 00:07:19)
- 16 (2015-12-26 (土) 15:54:52)
- 17 (2017-06-14 (水) 18:25:38)
- 18 (2018-06-12 (火) 09:58:49)
- 19 (2020-06-04 (木) 12:09:57)
- 20 (2021-11-18 (木) 11:29:54)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JTableのセルのハイライト #navi(../) *JTableのセルのハイライト [#z4e80d4e] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2004-03-21~ 更新日:&lastmod; #contents **概要 [#ub24dcf1] セル上にマウスがある場合、その色を変更します。 #screenshot **サンプルコード [#j4bc80b6] #code{{ class TestRenderer extends DefaultTableCellRenderer implements MouseListener, MouseMotionListener { private final JTable table; class HighlightListener extends MouseInputAdapter { //[JDK 6] MouseAdapter { private int row = -1; private int col = -1; public TestRenderer(JTable table) { super(); private final JTable table; public HighlightListener(JTable table) { 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 boolean isHighlightedCell(int row, int column) { return this.row==row && this.col==column; } @Override 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; row = -1; col = -1; } table.repaint(); } public void mouseExited(MouseEvent e) { row = -1; col = -1; @Override 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) {} } }} #code{{ class HighlightRenderer extends DefaultTableCellRenderer { private final HighlightListener highlighter; public HighlightRenderer(JTable table) { super(); highlighter = new HighlightListener(table); table.addMouseListener(highlighter); table.addMouseMotionListener(highlighter); } 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(highlighter.isHighlightedCell(row, column)) { setBackground(Color.RED); }else{ setBackground(isSelected?table.getSelectionBackground():table.getBackground()); } return this; } } }} -&jnlp; -&jar; -&zip; **解説 [#y0f7d171] MouseListener、MouseMotionListenerをimplementsしたセルレンダラーを作成し、マウスカーソルが乗っているセルの色を変更しています。 セルレンダラーにMouseListener、MouseMotionListenerを追加し、マウスカーソルが乗っているセルの色を変更しています。 JTable#prepareRendererをオーバーライドする場合も、同様の方法が使用できます。 #code{{ 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) { class HighlightableTable extends JTable { private final HighlightListener highlighter; public HighlightableTable(TableModel model) { super(model); addMouseListener(this); addMouseMotionListener(this); highlighter = new HighlightListener(this); addMouseListener(highlighter); addMouseMotionListener(highlighter); } @Override public Component prepareRenderer(TableCellRenderer renderer, int row, int column) { Component c = super.prepareRenderer(renderer, row, column); if(this.row==row && this.col==column) { if(highlighter.isHighlightedCell(row, 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) {} } }} **参考リンク [#a069e48b] -[[JListのセルをカーソル移動でロールオーバー>Swing/RollOverListener]] **コメント [#y73c58db] - ソースコードの整理、スクリーンショットの更新 -- [[terai]] &new{2008-03-14 (金) 16:38:10}; #comment