KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > event > TableModelEvent


1 /*
2  * @(#)TableModelEvent.java 1.21 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing.event;
9
10 import java.util.EventObject JavaDoc;
11 import javax.swing.table.*;
12
13 /**
14  * TableModelEvent is used to notify listeners that a table model
15  * has changed. The model event describes changes to a TableModel
16  * and all references to rows and columns are in the co-ordinate
17  * system of the model.
18  * Depending on the parameters used in the constructors, the TableModelevent
19  * can be used to specify the following types of changes: <p>
20  *
21  * <pre>
22  * TableModelEvent(source); // The data, ie. all rows changed
23  * TableModelEvent(source, HEADER_ROW); // Structure change, reallocate TableColumns
24  * TableModelEvent(source, 1); // Row 1 changed
25  * TableModelEvent(source, 3, 6); // Rows 3 to 6 inclusive changed
26  * TableModelEvent(source, 2, 2, 6); // Cell at (2, 6) changed
27  * TableModelEvent(source, 3, 6, ALL_COLUMNS, INSERT); // Rows (3, 6) were inserted
28  * TableModelEvent(source, 3, 6, ALL_COLUMNS, DELETE); // Rows (3, 6) were deleted
29  * </pre>
30  *
31  * It is possible to use other combinations of the parameters, not all of them
32  * are meaningful. By subclassing, you can add other information, for example:
33  * whether the event WILL happen or DID happen. This makes the specification
34  * of rows in DELETE events more useful but has not been included in
35  * the swing package as the JTable only needs post-event notification.
36  * <p>
37  * <strong>Warning:</strong>
38  * Serialized objects of this class will not be compatible with
39  * future Swing releases. The current serialization support is
40  * appropriate for short term storage or RMI between applications running
41  * the same version of Swing. As of 1.4, support for long term storage
42  * of all JavaBeans<sup><font size="-2">TM</font></sup>
43  * has been added to the <code>java.beans</code> package.
44  * Please see {@link java.beans.XMLEncoder}.
45  *
46  * @version 1.21 12/19/03
47  * @author Alan Chung
48  * @author Philip Milne
49  * @see TableModel
50  */

51 public class TableModelEvent extends java.util.EventObject JavaDoc
52 {
53     /** Identifies the addtion of new rows or columns. */
54     public static final int INSERT = 1;
55     /** Identifies a change to existing data. */
56     public static final int UPDATE = 0;
57     /** Identifies the removal of rows or columns. */
58     public static final int DELETE = -1;
59     
60     /** Identifies the header row. */
61     public static final int HEADER_ROW = -1;
62
63     /** Specifies all columns in a row or rows. */
64     public static final int ALL_COLUMNS = -1;
65     
66 //
67
// Instance Variables
68
//
69

70     protected int type;
71     protected int firstRow;
72     protected int lastRow;
73     protected int column;
74
75 //
76
// Constructors
77
//
78

79     /**
80      * All row data in the table has changed, listeners should discard any state
81      * that was based on the rows and requery the <code>TableModel</code>
82      * to get the new row count and all the appropriate values.
83      * The <code>JTable</code> will repaint the entire visible region on
84      * receiving this event, querying the model for the cell values that are visible.
85      * The structure of the table ie, the column names, types and order
86      * have not changed.
87      */

88     public TableModelEvent(TableModel source) {
89         // Use Integer.MAX_VALUE instead of getRowCount() in case rows were deleted.
90
this(source, 0, Integer.MAX_VALUE, ALL_COLUMNS, UPDATE);
91     }
92     
93     /**
94      * This row of data has been updated.
95      * To denote the arrival of a completely new table with a different structure
96      * use <code>HEADER_ROW</code> as the value for the <code>row</code>.
97      * When the <code>JTable</code> receives this event and its
98      * <code>autoCreateColumnsFromModel</code>
99      * flag is set it discards any TableColumns that it had and reallocates
100      * default ones in the order they appear in the model. This is the
101      * same as calling <code>setModel(TableModel)</code> on the <code>JTable</code>.
102      */

103     public TableModelEvent(TableModel source, int row) {
104     this(source, row, row, ALL_COLUMNS, UPDATE);
105     }
106     
107     /**
108      * The data in rows [<I>firstRow</I>, <I>lastRow</I>] have been updated.
109      */

110     public TableModelEvent(TableModel source, int firstRow, int lastRow) {
111     this(source, firstRow, lastRow, ALL_COLUMNS, UPDATE);
112     }
113     
114     /**
115      * The cells in column <I>column</I> in the range
116      * [<I>firstRow</I>, <I>lastRow</I>] have been updated.
117      */

118     public TableModelEvent(TableModel source, int firstRow, int lastRow, int column) {
119     this(source, firstRow, lastRow, column, UPDATE);
120     }
121     
122     /**
123      * The cells from (firstRow, column) to (lastRow, column) have been changed.
124      * The <I>column</I> refers to the column index of the cell in the model's
125      * co-ordinate system. When <I>column</I> is ALL_COLUMNS, all cells in the
126      * specified range of rows are considered changed.
127      * <p>
128      * The <I>type</I> should be one of: INSERT, UPDATE and DELETE.
129      */

130     public TableModelEvent(TableModel source, int firstRow, int lastRow, int column, int type) {
131     super(source);
132     this.firstRow = firstRow;
133     this.lastRow = lastRow;
134     this.column = column;
135     this.type = type;
136     }
137
138 //
139
// Querying Methods
140
//
141

142    /** Returns the first row that changed. HEADER_ROW means the meta data,
143      * ie. names, types and order of the columns.
144      */

145     public int getFirstRow() { return firstRow; };
146
147     /** Returns the last row that changed. */
148     public int getLastRow() { return lastRow; };
149     
150     /**
151      * Returns the column for the event. If the return
152      * value is ALL_COLUMNS; it means every column in the specified
153      * rows changed.
154      */

155     public int getColumn() { return column; };
156     
157     /**
158      * Returns the type of event - one of: INSERT, UPDATE and DELETE.
159      */

160     public int getType() { return type; }
161 }
162
Popular Tags