001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: TableRowMouseAdapter.java,v $ 023 Revision 1.3 2004/05/05 22:19:12 markl 024 renamed methods, updated comment block 025 026 Revision 1.2 2004/03/22 06:55:57 markl 027 provide mouse button number to handler methods 028 029 Revision 1.1 2004/03/10 00:53:42 markl 030 new class 031 ---------------------------------------------------------------------------- 032*/ 033 034package kiwi.event; 035 036import java.awt.*; 037import java.awt.event.*; 038import javax.swing.*; 039 040/** A simple extension of <code>MouseAdapter</code> for handling single- 041 * and double-clicks on rows in a <code>JTable</code>. 042 * 043 * <p> 044 * The following example illustrates how this adapter might be used: 045 * <p> 046 * <pre> 047 * table.addMouseListener(new TableRowMouseAdapter() 048 * { 049 * public void rowClicked(int row, int button) 050 * { 051 * System.out.println("Button " + button + " clicked on row #" + row); 052 * } 053 * }); 054 * </pre> 055 * 056 * @author Mark Lindner 057 * @since Kiwi 2.0 058 */ 059 060public class TableRowMouseAdapter extends MouseAdapter 061 { 062 063 /** Construct a new <code>TableRowMouseAdapter</code>. */ 064 065 public TableRowMouseAdapter() 066 { 067 } 068 069 /** Handle a mouse event. This method dispatches the mouse event to one of 070 * the click handlers, based on its click count. It is assumed that the 071 * source of the event is an instance of <code>JTable</code>; if it is not, 072 * the event is ignored. 073 * 074 * @param evt The event. 075 */ 076 077 public final void mouseClicked(MouseEvent evt) 078 { 079 Object source = evt.getSource(); 080 if(!(source instanceof JTable)) 081 return; 082 083 JTable table = (JTable)source; 084 085 int selRow = table.rowAtPoint(new Point(evt.getX(), evt.getY())); 086 if(selRow != -1) 087 { 088 switch(evt.getClickCount()) 089 { 090 case 1: 091 rowClicked(selRow, evt.getButton()); 092 break; 093 094 case 2: 095 rowDoubleClicked(selRow, evt.getButton()); 096 break; 097 } 098 } 099 } 100 101 /** Handle a single-click on a row. 102 * 103 * @param row The index of the row that was clicked on. 104 * @param button The mouse button that was clicked. 105 */ 106 107 public void rowClicked(int row, int button) 108 { 109 } 110 111 /** Handle a double-click on a row. 112 * 113 * @param row The index of the row that was clicked on. 114 * @param button The mouse button that was clicked. 115 */ 116 117 public void rowDoubleClicked(int row, int button) 118 { 119 } 120 121 } 122 123/* end of source file */