forked from yrojha4ever/JavaStud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableTest.java
More file actions
86 lines (72 loc) · 2.19 KB
/
TableTest.java
File metadata and controls
86 lines (72 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package test.java;
import java.awt.EventQueue;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class TableTest {
public static void main( String[] args ) {
new TableTest( );
}
public TableTest( ) {
startUI( );
}
public void startUI( ) {
EventQueue.invokeLater( new Runnable( ) {
@Override
public void run( ) {
try {
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName( ) );
} catch ( ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex ) {
ex.printStackTrace( );
}
MyTableModel model = new MyTableModel( );
model.addRow( new Object[] { 0, "Brian", false } );
model.addRow( new Object[] { 1, "Ned", false } );
model.addRow( new Object[] { 2, "John", false } );
model.addRow( new Object[] { 3, "Drogo", false } );
JTable table = new JTable( model );
JFrame frame = new JFrame( "Testing" );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( new JScrollPane( table ) );
frame.pack( );
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
} );
}
public class MyTableModel extends DefaultTableModel {
public MyTableModel( ) {
super( new String[] { "ID", "Name", "Present" }, 0 );
}
@Override
public Class< ? > getColumnClass( int columnIndex ) {
Class clazz = String.class;
switch ( columnIndex ) {
case 0:
clazz = Integer.class;
break;
case 2:
clazz = Boolean.class;
break;
}
return clazz;
}
@Override
public boolean isCellEditable( int row, int column ) {
return column == 2;
}
@Override
public void setValueAt( Object aValue, int row, int column ) {
if ( aValue instanceof Boolean && column == 2 ) {
System.out.println( aValue );
Vector rowData = ( Vector ) getDataVector( ).get( row );
rowData.set( 2, ( boolean ) aValue );
fireTableCellUpdated( row, column );
}
}
}
}