forked from cstrahan/aduni
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableTest.java
More file actions
81 lines (68 loc) · 1.75 KB
/
TableTest.java
File metadata and controls
81 lines (68 loc) · 1.75 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
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
/**
* Main class for game. Does window stuff and display
*/
class MyFrame extends JFrame{
public MyFrame(){
// set up UI
setTitle("MyFrame");
setSize(200,200); // size in pixels
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// make model
MyTableModel model = new MyTableModel(31);
// make table
JTable table = new JTable(model);
// Random stuff we just have to do (the book explains, kind of)
Container contentPane = getContentPane();
// add panel with scroller
JScrollPane scroll = new JScrollPane(table);
contentPane.add(scroll);
}
}
class MyTableModel extends AbstractTableModel{
int rows = 2;
int cols = 2;
String[] colnames = {"Day","Hours"};
Integer[] hours;
MyTableModel(int rows){
this.rows = rows;
hours = new Integer[rows];
for(int i=0;i<rows;i++){
hours[i] = new Integer((int)(Math.random() * 8.0));
}
}
public int getColumnCount(){
return(cols);
}
public String getColumnName(int c){
return(colnames[c]);
}
public int getRowCount(){
return(rows);
}
public Object getValueAt(int r, int c){
if(c == 0) return(new Integer(r));
else return(hours[r]);
}
public void setValueAt(Object value, int r, int c){
hours[r] = new Integer((String)value);
}
// make second column editable
public boolean isCellEditable(int r, int c){
if(c > 0) return(true);
return(false);
}
}
public class TableTest{
// OK let's have our main() create a frame.
public static void main(String[] args){
MyFrame myframe = new MyFrame();
myframe.show();
}
}
/*
*/