-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathaddressFrame.java
265 lines (217 loc) · 7.84 KB
/
addressFrame.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package comp421;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.*;
import javax.swing.table.*;
import java.text.SimpleDateFormat;
public class addressFrame extends JPanel
{
int userid;
int orderNumber;
int addrid[] = null;
SQL sql;
Object[][] data = null;
JTable table = null;
JButton ConfirmButton = new JButton("Confirm");
MainFrame mainFrame = null;
JFrame frame = null;
java.sql.ResultSet rs = null;
public addressFrame(int id, SQL sql,int orderNumber,MainFrame mainFrame) throws SQLException
{
userid = id;
this.orderNumber = orderNumber;
this.sql = sql;
this.mainFrame= mainFrame;
String sqlCode = "select addrid, name, province, city, streetaddr, postCode from address where userid = " + userid;
rs = sql.QueryExchte(sqlCode);
table = new JTable(new AddressModule());
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(5).setCellEditor(new DefaultCellEditor(new JCheckBox()));
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(e.getClickCount() == 1){
int columnIndex = table.columnAtPoint(e.getPoint()); //获取点击的列
int rowIndex = table.rowAtPoint(e.getPoint()); //获取点击的行
if(columnIndex == 5) {//第0列时,执行代码
if(table.getValueAt(rowIndex,columnIndex) == null){ //如果未初始化,则设置为false
table.setValueAt(false, rowIndex, columnIndex);
}
if(((Boolean)table.getValueAt(rowIndex,columnIndex)).booleanValue()){ //原来选中
table.setValueAt(false, rowIndex ,5); //点击后,取消选中
}
else {//原来未选中
table.setValueAt(true, rowIndex, 5);
}
}
}
}
});
initColumnSizes(table);
JScrollPane scrollPane= new JScrollPane(table);
add(scrollPane);
}
private int getSize(java.sql.ResultSet resultSet)
{
int rowCount = 0;
try {
resultSet.last();
rowCount = resultSet.getRow();
resultSet.first();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return rowCount;
}
private void initColumnSizes(JTable table)
{/*
AddressModule model = (AddressModule)table.getModel();
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int cellWidth = 0;
Object longValue[] = model.longValue;
TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
for(int i = 0; i<5; i++)
{
column = table.getColumnModel().getColumn(i);
comp = headerRenderer.getTableCellRendererComponent(table, longValue[i], false, false, 0, 0);
headerWidth = comp.getPreferredSize().width;
/*comp = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(
table, longValue[i],
false, false, 0, i);
cellWidth = comp.getPreferredSize().width;
column.setPreferredWidth(Math.max(headerWidth, cellWidth));
}*/
}
public class AddressModule extends AbstractTableModel
{
String[] columnName = {"Name","Province","City","Street Address","Post Code", "select"};
Object[][] data = null;
Object[] longValue = {"Kitra N. Cabrera", "New Brunswick", "Campbellton", "P.O. Box 268, 4081 Suspendisse Street", "E2J 3G6", new Boolean(false)};
public AddressModule() throws SQLException
{
super();
data = (Object[][]) initData();
}
@Override
public int getColumnCount() {
return columnName.length;
}
public boolean isCellEditable(int row, int column)
{
// 带有按钮列的功能这里必须要返回true不然按钮点击时不会触发编辑效果,也就不会触发事件。
if (column == 5)
{
return true;
}
else
{
return false;
}
}
@Override
public String getColumnName(int col) {
return columnName[col];
}
public void setValueAt(Object o, int rowIndex, int columnIndex)
{
data[rowIndex][columnIndex] = o;
table.repaint();
}
@Override
public int getRowCount() {
return data.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
private Object[] initData() throws SQLException
{
int count = 0;
Object[][] data = null;
if(rs != null)
{
data = new Object[getSize(rs)][6];
addrid = new int[getSize(rs)];
if(data.length!=0 && rs.first())
do
{
addrid[count] = rs.getInt(1);
data[count][0] = rs.getString(2); //name
data[count][1] = rs.getString(3); //province
data[count][2] = rs.getString(4); //city
data[count][3] = rs.getString(5); //street address
data[count][4] = rs.getString(6); //postCode
data[count][5] = new Boolean(false);//checkbox
count++;
}while(rs.next());
}
return data;
}
}
public void createUI(int id, SQL sqlo) throws SQLException
{
JPanel titlePanel = new JPanel(new GridLayout(1,0));
titlePanel.add(new JLabel("Address list"));
JPanel buttonPanel = new JPanel(new GridLayout(1,2,25,25));
Listener listener = new Listener();
ConfirmButton.addActionListener(listener);
buttonPanel.add(ConfirmButton);
JFrame frame = new JFrame("TableRenderDemo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.frame = frame;
frame.add(titlePanel,BorderLayout.NORTH);
frame.add(this,BorderLayout.CENTER);
frame.add(buttonPanel,BorderLayout.SOUTH);
//Display the window.
frame.pack();
frame.setVisible(true);
}
static public void invoke(int id, SQL sql,int orderNumber,MainFrame mainFrame) throws SQLException
{
addressFrame frame = new addressFrame(id,sql,orderNumber,mainFrame);
frame.createUI(id, sql);
}
private class Listener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == ConfirmButton)
{
//insert into Deliver_To
//values(addrid, orderNumber, TimeDelivered);
int flag = 0;
int selected = -1;
// add jusdgement if there is two selected;
for (int i =0;i <getSize(rs);i++)
if((boolean) table.getValueAt(i, 5))
{
flag ++;
selected = i;
}
if(flag != 1)
JOptionPane.showMessageDialog(null, "You must select one and only one address to confirm the delivery", "Error",JOptionPane.OK_OPTION);
else
{
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String sqlCode = "insert into Deliver_To values("+addrid[selected]+","+orderNumber+", \'"+sdf.format(d)+"\');";
sql.WriteExcute(sqlCode);
JOptionPane.showMessageDialog(null, "Your ordered is confirmed. Thank you for your purchase!", "Success", JOptionPane.INFORMATION_MESSAGE,new ImageIcon(addressFrame.class.getResource("success.png")));
mainFrame.setVisible(true);
frame.dispose();
}
}
}
}
}