-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateStudent.java
More file actions
240 lines (234 loc) · 10.2 KB
/
Copy pathUpdateStudent.java
File metadata and controls
240 lines (234 loc) · 10.2 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
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
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class UpdateStudent extends JFrame implements ActionListener {
JButton ok, btnOK, btnCancel, btnBack;
JTextField jTextField, tf1, tf2, tf3, tf4;
JTextField jIDf;
UpdateStudent() throws IOException {
Container con = this.getContentPane();
// Setting con
con.setLayout(new BorderLayout());
//label Panel
JPanel jTopTop1 = new JPanel();
jTopTop1.setLayout(new BoxLayout(jTopTop1, BoxLayout.PAGE_AXIS));
JLabel jtop = new JLabel("Update Student", JLabel.CENTER);
jtop.setForeground(new Color(222, 52, 49));
jtop.setFont(new Font("Courier", Font.BOLD, 20));
JPanel jpTop = new JPanel();
jpTop.setLayout(new BoxLayout(jpTop, BoxLayout.PAGE_AXIS));
JPanel jInfor = new JPanel();
jInfor.setLayout(new FlowLayout(FlowLayout.CENTER));
JLabel jId = new JLabel("ID");
jIDf = new JTextField(10);
jInfor.add(jId);
jInfor.add(jIDf);
jtop.setAlignmentX(CENTER_ALIGNMENT);
jpTop.add(Box.createRigidArea(new Dimension(0, 30)));
jpTop.add(jtop);
jpTop.add(Box.createRigidArea(new Dimension(0, 50)));
jpTop.add(jInfor);
jInfor.setAlignmentX(Component.CENTER_ALIGNMENT);
ok = new JButton("OK");
ok.setAlignmentX(Component.CENTER_ALIGNMENT);
jInfor.add(ok);
//toppanel
JPanel jTopTop = new JPanel();
jTopTop.setLayout(new BoxLayout(jTopTop, BoxLayout.PAGE_AXIS));
//
JPanel jLine1 = new JPanel();
jLine1.setLayout(new BoxLayout(jLine1, BoxLayout.LINE_AXIS));
JLabel studentName = new JLabel("Name:");
jLine1.add(Box.createRigidArea(new Dimension(20, 0)));
jTextField = new JTextField();
jTextField.setPreferredSize(new Dimension(0, 20));
jLine1.add(studentName);
jLine1.add(Box.createRigidArea(new Dimension(30, 0)));
jLine1.add(jTextField);
jLine1.add(Box.createRigidArea(new Dimension(10, 0)));
//-------
JPanel jLine2 = new JPanel();
jLine2.setLayout(new BoxLayout(jLine2, BoxLayout.LINE_AXIS));
jLine2.add(Box.createRigidArea(new Dimension(20, 0)));
JLabel gradeLb = new JLabel("Grade:");
tf1 = new JTextField();
tf1.setPreferredSize(new Dimension(0, 20));
int distance = studentName.getPreferredSize().width - gradeLb.getPreferredSize().width;
jLine2.add(Box.createRigidArea(new Dimension(distance, 0)));
jLine2.add(gradeLb);
jLine2.add(Box.createRigidArea(new Dimension(30, 0)));
jLine2.add(tf1);
jLine2.add(Box.createRigidArea(new Dimension(10, 0)));
//--------
JPanel jLine3 = new JPanel();
jLine3.setLayout(new BoxLayout(jLine3, BoxLayout.LINE_AXIS));
jLine3.add(Box.createRigidArea(new Dimension(20, 0)));
JLabel imagelb = new JLabel("Image:");
tf2 = new JTextField();
tf2.setText("C://");
tf2.setPreferredSize(new Dimension(0, 20));
jLine3.add(imagelb);
jLine3.add(Box.createRigidArea(new Dimension(28, 0)));
jLine3.add(tf2);
jLine3.add(Box.createRigidArea(new Dimension(10, 0)));
//
JPanel jLine4 = new JPanel();
jLine4.setLayout(new BoxLayout(jLine4, BoxLayout.LINE_AXIS));
jLine4.add(Box.createRigidArea(new Dimension(20, 0)));
JLabel addressLb = new JLabel("Address:");
tf3 = new JTextField();
tf3.setPreferredSize(new Dimension(0, 20));
jLine4.add(addressLb);
jLine4.add(Box.createRigidArea(new Dimension(16, 0)));
jLine4.add(tf3);
jLine4.add(Box.createRigidArea(new Dimension(10, 0)));
//
JPanel jLine5 = new JPanel();
jLine5.setLayout(new BoxLayout(jLine5, BoxLayout.LINE_AXIS));
jLine5.add(Box.createRigidArea(new Dimension(20, 0)));
JLabel noteLb = new JLabel("Notes:");
tf4 = new JTextField();
tf4.setPreferredSize(new Dimension(0, 20));
jLine5.add(noteLb);
jLine5.add(Box.createRigidArea(new Dimension(30, 0)));
jLine5.add(tf4);
jLine5.add(Box.createRigidArea(new Dimension(10, 0)));
//--------
jTopTop.add(Box.createRigidArea(new Dimension(0, 30)));
jTopTop.add(jLine1);
jTopTop.add(Box.createRigidArea(new Dimension(0, 30)));
jTopTop.add(jLine2);
jTopTop.add(Box.createRigidArea(new Dimension(0, 30)));
jTopTop.add(jLine3);
jTopTop.add(Box.createRigidArea(new Dimension(0, 30)));
jTopTop.add(jLine4);
jTopTop.add(Box.createRigidArea(new Dimension(0, 30)));
jTopTop.add(jLine5);
//bottom panel
JPanel botRealPan = new JPanel();
botRealPan.setLayout(new BoxLayout(botRealPan, BoxLayout.PAGE_AXIS));
JPanel botPanel = new JPanel();
botPanel.setLayout(new BoxLayout(botPanel, BoxLayout.LINE_AXIS));
botRealPan.add(Box.createRigidArea(new Dimension(0, 10)));
btnOK = new JButton("OK");
btnCancel = new JButton("Cancle");
btnBack = new JButton("Back");
btnOK.addActionListener(this);
btnCancel.addActionListener(this);
btnBack.addActionListener(this);
ok.addActionListener(this);
botPanel.add(btnOK);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
botPanel.add(btnCancel);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
botPanel.add(btnBack);
botPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
botRealPan.add(botPanel);
botRealPan.setAlignmentX(Component.CENTER_ALIGNMENT);
btnOK.setEnabled(false);
botRealPan.add(Box.createRigidArea(new Dimension(0, 10)));
botPanel.setAlignmentX(CENTER_ALIGNMENT);
//--------
JPanel jPanel=new JPanel();
jPanel.setAlignmentX(CENTER_ALIGNMENT);
String[][] data=ConnectToDatabase.getList();
String[] column={"ID","Name","Grade","Image","Address","Notes"};
DefaultTableModel model = new DefaultTableModel(data, column);
JTable jtable=new JTable(model);
JScrollPane sp = new JScrollPane(jtable);
jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.LINE_AXIS));
jPanel.add(sp);
//
con.add(jpTop, BorderLayout.PAGE_START);
con.add(jTopTop, BorderLayout.CENTER);
con.add(botRealPan, BorderLayout.PAGE_END);
con.add(jPanel,BorderLayout.LINE_END);
// Setting JFrame
this.setTitle("Update Student");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(new Dimension(800, 550));
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(dim.width / 2 - this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2);
}
public static void GUI() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UpdateStudent frame = new UpdateStudent();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(btnCancel)) {
this.dispose();
UpdateStudent.GUI();
} else if (e.getSource().equals(ok)) {
String id = jIDf.getText().toString();
try {
if (ConnectToDatabase.checkStudent(id)) {
String[] data = ConnectToDatabase.inforUser(id);
jTextField.setText(data[1]);
tf1.setText(data[2]);
tf2.setText(data[3]);
tf3.setText(data[4]);
tf4.setText(data[5]);
ok.setEnabled(false);
btnOK.setEnabled(true);
} else {
JOptionPane.showMessageDialog(null, "ID do not existed.", "Alert", JOptionPane.WARNING_MESSAGE);
jIDf.setText("");
}
} catch (IOException ex) {
ex.printStackTrace();
}
} else if (e.getSource().equals(btnOK)) {
try {
//
boolean check = true;
String numberValue = tf1.getText().toString();
Float value = Float.valueOf(numberValue).floatValue();
if (value < 0 || value > 4) check = false;
String fileF = tf2.getText().toString();
File fileFF = new File(fileF);
//
if (check == true) {
if (fileFF.exists() && fileFF.isFile()) {
if (ConnectToDatabase.UpdateUser(jIDf.getText().toString(),
jTextField.getText().toString(), Float.valueOf(tf1.getText().toString()).floatValue(),
tf2.getText().toString(), tf3.getText().toString(), tf4.getText().toString())) {
JOptionPane.showMessageDialog(null, "Successfully Updated.", "Alert", JOptionPane.WARNING_MESSAGE);
this.dispose();
UpdateStudent.GUI();
} else {
JOptionPane.showMessageDialog(null, "Failed Updated.", "Alert", JOptionPane.WARNING_MESSAGE);
this.dispose();
UpdateStudent.GUI();
}
} else {
JOptionPane.showMessageDialog(null, "File is not exist, please input correct file in computer! ");
this.dispose();
UpdateStudent.GUI();
}
}else{
JOptionPane.showMessageDialog(null, "Wrong type for grade! Input again");
this.dispose();
UpdateStudent.GUI();
}
} catch (IOException ex) {
ex.printStackTrace();
}
} else if (e.getSource().equals(btnBack)) {
this.dispose();
Menu.GUI();
}
}
}