-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathJTableDemo.java
64 lines (59 loc) · 1.61 KB
/
JTableDemo.java
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
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class JTableDemo extends JFrame {
private static class SymRenderer extends DefaultTableCellRenderer {
public SymRenderer() { super(); }
@Override public void setValue(Object value) {
Object col = value;
if (value != null && value instanceof Stock) {
Stock s = (Stock)value;
col = value;
}
super.setValue(col);
}
}
private Model1 data;
public JTableDemo() {
super("JTable Demo");
setSize(800, 600);
Container c = getContentPane();
data = new Model1();
Font f = new Font("Arial", Font.BOLD, 32);
JTable table = new JTable(data); //, (Object[])data.getColumnNames());
table.setFont(f);
table.setRowHeight(120);
TableColumnModel cm = table.getColumnModel();
TableColumn tc = cm.getColumn(0);
tc.setCellRenderer(new SymRenderer());
c.add(new JScrollPane(table), BorderLayout.CENTER);
data.add(new Stock("IBM", 104.5, 1.2));
data.add(new Stock("AAPL", 111.24, 1.9));
setVisible(true);
}
public static void main(String[] args) {
new JTableDemo();
}
}
class Model1 extends AbstractTableModel {
private ArrayList<Stock> rows;
private String[] columnNames;
public Model1() {
columnNames = new String[] {
"Symbol", "Price", "Dividend"
};
rows = new ArrayList<Stock>();
}
public String[] getColumnNames() { return columnNames; }
public void add(Stock s) {
rows.add(s);
}
public void remove(Stock s) {
}
public Object getValueAt(int row, int col) {
return rows.get(row);
}
public int getColumnCount() { return 3; }
public int getRowCount() { return rows.size(); }
}