-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathSaveToCartFrame.java
394 lines (294 loc) · 11.1 KB
/
SaveToCartFrame.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package comp421;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.swing.table.*;
//need to add button back -done
//sql code to be implemented in button editor -done
// case no result is empty, will not have table, but implement an panel with notice
public class SaveToCartFrame extends JPanel {
int userid;
SQL sql;
java.sql.ResultSet rs = null;
JTable table = null;
JButton backButton = new JButton("Back");
SearchFrame searchFrame = null;
public SaveToCartFrame(int id,SQL sqlo,java.sql.ResultSet rs,SearchFrame searchFrame) throws SQLException{
super(new GridLayout(1,0));
userid = id;
sql = sqlo;
this.rs = rs;
this.searchFrame = searchFrame;
table = new JTable(new goodsTableModule());
table.setPreferredSize(new Dimension(1400,70));
table.setFillsViewportHeight(true);
table.setRowHeight(50);
JScrollPane scrollPane = new JScrollPane(table);
initColumnSizes(table);
setUpAddCartColumn(table,table.getColumnModel().getColumn(9));
this.add(scrollPane);
// to be editted
}
private void setUpAddCartColumn(JTable table, TableColumn column) throws SQLException {
column.setCellEditor(new MyButtonEditor());
column.setCellRenderer(new MyButtonRender());
}
public class MyButtonRender implements TableCellRenderer
{
private JPanel panel;
private JButton button;
public MyButtonRender()
{
this.initButton();
this.initPanel();
// 添加按钮。
this.panel.add(this.button);
}
private void initButton()
{
this.button = new JButton("Add");
this.button.setBounds(10, 10, 80, 30);
}
private void initPanel()
{
this.panel = new JPanel();
this.panel.setLayout(null);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
return this.panel;
}
}
public class MyButtonEditor extends DefaultCellEditor
{
private JButton button;
private JPanel panel;
public MyButtonEditor()
{
super(new JTextField());
this.setClickCountToStart(1);
this.initButton();
this.initPanel();
this.panel.add(this.button);
}
private void initButton()
{
this.button = new JButton("Add");
// set size and positon
this.button.setBounds(10, 10, 80, 30);
this.button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int row = table.getSelectedRow();
/*insert into Save_To_Shopping_Cart
values(userid, pid, addTime, quantity);*/
String code = "select quantity from Save_To_Shopping_Cart Where userid = "+userid+" and pid = "+table.getValueAt(row, 0)+";";
java.sql.ResultSet result = sql.QueryExchte(code);
String sqlCode = "";
if(getSize(result)==0)
{
sqlCode += "insert into";
sqlCode += " Save_To_Shopping_Cart values(";
sqlCode += userid + ", ";
sqlCode += table.getValueAt(row, 0)+", ";// pid
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sqlCode +="\'"+ sdf.format(d)+"\'"+ ", ";
sqlCode += "1);";
}
else
{
try {
sqlCode += "UPDATE Save_To_Shopping_Cart Set quantity = " + (result.getInt(1)+1);
sqlCode += " where userid = "+ userid + " and pid = "+ table.getValueAt(row, 0)+";";
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
sql.WriteExcute(sqlCode);
}
});
}
private void initPanel()
{
this.panel = new JPanel();
this.panel.setLayout(null);
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
{
return this.panel;
}
}
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) {
TableColumn column = null;
for(int i = 0;i<10;i++)
{
column = table.getColumnModel().getColumn(i);
if(i == 1|| i == 0)
column.setPreferredWidth(40);
if(i == 2)
column.setPreferredWidth(120);
if(i == 3)
column.setPreferredWidth(450);
if(i == 4|| i ==5)
column.setPreferredWidth(200);
if(i == 6)
column.setPreferredWidth(100);
if(i == 7|| i == 8)
column.setPreferredWidth(75);
if(i == 9)
column.setPreferredWidth(100);
}
/* goodsTableModule model = (goodsTableModule)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<9; 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));
}*/
}
class goodsTableModule extends AbstractTableModel{
private String[] columnNames={"product ID","Shop ID","brand","Product Name","Type","Model Number","color"
,"amount","Price","Add to Cart"};
private Object[][] data;
public final Object[] longValue = {new Integer(10),new Integer(100),"Microsoft","'DELL Ultra HD 4k Monitor P2715Q 27-Inch Screen LED-Lit Monitor",
"computer accessories","759944-0010","black","100","1000","100000"};
public goodsTableModule() throws SQLException
{
super();
data = initData();
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return columnNames.length;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return data.length;
}
@Override
public String getColumnName(int col) {
return columnNames[col];
}
@Override
public Object getValueAt(int row, int col) {
// TODO Auto-generated method stub
//System.out.println("get data at row " + row +" column "+ col);
return data[row][col];
}
@Override
public boolean isCellEditable(int row, int column)
{
// 带有按钮列的功能这里必须要返回true不然按钮点击时不会触发编辑效果,也就不会触发事件。
if (column == 9)
{
return true;
}
else
{
return false;
}
}
public Object[][] initData() throws SQLException
{
int count = 0;
Object[][] data = null;
if(rs != null)
{
data = new Object[getSize(rs)][10];
if(data.length!=0)
do
{
data[count][0] = rs.getInt(1);
data[count][1] = rs.getInt(2);
data[count][2] = rs.getString(3);
data[count][3] = rs.getString(4);
data[count][4] = rs.getString(5);
data[count][5] = rs.getString(6);
data[count][6] = rs.getString(7);
data[count][7] = rs.getInt(8);
data[count][8] = rs.getDouble(9);
data[count][9] = "";
count++;
} while(rs.next());
else
return null;
}
return data;
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
private void createUI(int id, SQL sqlo,java.sql.ResultSet rs) throws SQLException
{
JFrame frame = new JFrame("TableRenderDemo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Create and set up the content pane.
/* *newContentPane.setOpaque(true); //content panes must be opaque
*frame.setContentPane(newContentPane);
*/
JPanel titlePanel = new JPanel(new GridLayout(1,1));
titlePanel.add(new JLabel("Select products to build order"));
JPanel buttonPanel = new JPanel(new GridLayout(1,1));
// buttonPanel.setLayout(null);
backButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
searchFrame.setVisible(true);
frame.dispose();
//other implements on close
}
});
backButton.setBounds(400, 50, 200, 50);
buttonPanel.add(backButton);
SaveToCartFrame newContentPane = new SaveToCartFrame(id, sqlo,rs, searchFrame);
frame.add(titlePanel,BorderLayout.NORTH);
frame.add(this,BorderLayout.CENTER);
//buttonPanel.setSize(1000,200);
frame.add(buttonPanel,BorderLayout.SOUTH);
frame.setSize(1400,800);
//Display the window.
frame.setVisible(true);
}
public static void invoke(int id,SQL sql, java.sql.ResultSet rs,SearchFrame searchFrame) throws SQLException
{
SaveToCartFrame frame = new SaveToCartFrame(id,sql,rs,searchFrame);
frame.createUI(id, sql, rs);
}
}