3131
3232package test .components ;
3333
34+ import java .awt .Color ;
35+ import java .awt .Component ;
36+ import java .awt .Dimension ;
37+ import java .awt .GridLayout ;
38+ import java .awt .event .MouseEvent ;
39+ import java .awt .event .MouseListener ;
40+
3441/*
3542 * TableDemo.java requires no other files.
3643 */
4552import javax .swing .table .AbstractTableModel ;
4653import javax .swing .table .TableCellRenderer ;
4754
48- import java .awt .Color ;
49- import java .awt .Component ;
50- import java .awt .Dimension ;
51- import java .awt .Font ;
52- import java .awt .Graphics ;
53- import java .awt .GridLayout ;
54- import java .awt .event .MouseEvent ;
55- import java .awt .event .MouseListener ;
56-
5755/**
5856 * TableDemo is just like SimpleTableDemo, except that it uses a custom
5957 * TableModel.
@@ -90,37 +88,6 @@ public TableDemo() {
9088
9189
9290 JTable table = new JTable (new MyTableModel ());
93- table .addMouseListener (new MouseListener () {
94-
95- @ Override
96- public void mouseClicked (MouseEvent e ) {
97- // TODO Auto-generated method stub
98-
99- }
100-
101- @ Override
102- public void mousePressed (MouseEvent e ) {
103- // TODO Auto-generated method stub
104-
105- }
106-
107- @ Override
108- public void mouseReleased (MouseEvent e ) {
109- // TODO Auto-generated method stub
110-
111- }
112-
113- @ Override
114- public void mouseEntered (MouseEvent e ) {
115- System .out .println ("Table mouse entered " + e .getSource ().getClass ().getName ());
116- }
117-
118- @ Override
119- public void mouseExited (MouseEvent e ) {
120- System .out .println ("Table mouse exited " + e .getSource ().getClass ().getName ());
121- }
122-
123- });
12491 table .setRowHeight (40 );
12592 table .setPreferredScrollableViewportSize (new Dimension (500 , 70 ));
12693 table .setFillsViewportHeight (true );
@@ -162,18 +129,22 @@ class MyTableModel extends AbstractTableModel {
162129 { ++i , color , "Joe" , "Brown" , "Pool" , new Integer (10 ), new Boolean (false ) }
163130 };
164131
132+ @ Override
165133 public int getColumnCount () {
166134 return columnNames .length ;
167135 }
168136
137+ @ Override
169138 public int getRowCount () {
170139 return data .length ;
171140 }
172141
142+ @ Override
173143 public String getColumnName (int col ) {
174144 return columnNames [col ];
175145 }
176146
147+ @ Override
177148 public Object getValueAt (int row , int col ) {
178149 return data [row ][col ];
179150 }
@@ -183,13 +154,15 @@ public Object getValueAt(int row, int col) {
183154 * cell. If we didn't implement this method, then the last column would contain
184155 * text ("true"/"false"), rather than a check box.
185156 */
157+ @ Override
186158 public Class getColumnClass (int c ) {
187159 return getValueAt (0 , c ).getClass ();
188160 }
189161
190162 /*
191163 * Don't need to implement this method unless your table's editable.
192164 */
165+ @ Override
193166 public boolean isCellEditable (int row , int col ) {
194167 // Note that the data/cell address is constant,
195168 // no matter where the cell appears onscreen.
@@ -203,6 +176,7 @@ public boolean isCellEditable(int row, int col) {
203176 /*
204177 * Don't need to implement this method unless your table's data can change.
205178 */
179+ @ Override
206180 public void setValueAt (Object value , int row , int col ) {
207181 if (DEBUG ) {
208182 System .out .println ("Setting value at " + row + "," + col + " to " + value + " (an instance of "
@@ -256,6 +230,7 @@ public static void main(String[] args) {
256230 // Schedule a job for the event-dispatching thread:
257231 // creating and showing this application's GUI.
258232 javax .swing .SwingUtilities .invokeLater (new Runnable () {
233+ @ Override
259234 public void run () {
260235 createAndShowGUI ();
261236 }
0 commit comments