-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddStudent.java
More file actions
195 lines (190 loc) · 8.43 KB
/
Copy pathAddStudent.java
File metadata and controls
195 lines (190 loc) · 8.43 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
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
public class AddStudent extends JFrame implements ActionListener {
JButton btnOK, btnCancel;
JTextField jTextFieldid, jTextField, tf1, tf2, tf3, tf4;
AddStudent() throws IOException {
Container con = this.getContentPane();
// Setting con
con.setLayout(new BorderLayout());
//label Panel
JLabel lb = new JLabel("Add Student", JLabel.CENTER);
lb.setForeground(new Color(222, 52, 49));
lb.setFont(new Font("Monaco", Font.PLAIN, 30));
//toppanel
JPanel jTopTop = new JPanel();
jTopTop.setLayout(new BoxLayout(jTopTop, BoxLayout.PAGE_AXIS));
//
JPanel jLineID = new JPanel();
jLineID.setLayout(new BoxLayout(jLineID, BoxLayout.LINE_AXIS));
JLabel ID = new JLabel("ID:");
jLineID.add(Box.createRigidArea(new Dimension(41, 0)));
jTextFieldid = new JTextField();
jTextFieldid.setPreferredSize(new Dimension(0, 20));
String idGetter=ConnectToDatabase.idGetter();
if (idGetter.equals(""))
jTextFieldid.setText("19121000");
jTextFieldid.setText(idGetter);
jTextFieldid.setEditable(false);
jLineID.add(ID);
jLineID.add(Box.createRigidArea(new Dimension(30, 0)));
jLineID.add(jTextFieldid);
jLineID.add(Box.createRigidArea(new Dimension(10, 0)));
//
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(jLineID);
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");
btnOK.addActionListener(this);
btnCancel.addActionListener(this);
botPanel.add(btnOK);
botPanel.add(Box.createRigidArea(new Dimension(10, 0)));
botPanel.add(btnCancel);
botRealPan.add(botPanel);
botRealPan.add(Box.createRigidArea(new Dimension(0, 10)));
botPanel.setAlignmentX(CENTER_ALIGNMENT);
//--------
con.add(lb, BorderLayout.PAGE_START);
con.add(jTopTop, BorderLayout.CENTER);
con.add(botRealPan, BorderLayout.PAGE_END);
// Setting JFrame
this.setTitle("Add Student");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setSize(new Dimension(450, 450));
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 {
AddStudent frame = new AddStudent();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(btnOK)) {
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 (fileFF.isFile()&&fileFF.exists()) {
if (check == true) {
if (jTextField.getText().isBlank() || tf1.getText().isBlank()
|| tf2.getText().isBlank() || tf3.getText().isBlank()) {
JOptionPane.showMessageDialog(null, "Blank Input", "Alert", JOptionPane.WARNING_MESSAGE);
} else {
try {
ConnectToDatabase.AddStudent(jTextFieldid.getText().toString(), jTextField.getText().toString(),
Float.valueOf(tf1.getText().toString()).floatValue(), tf2.getText().toString(),
tf3.getText().toString(), tf4.getText().toString());
JOptionPane.showMessageDialog(null, "Inserted Successfully.");
this.dispose();
AddStudent.GUI();
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Inserted Fail.");
this.dispose();
}
}
} else {
JOptionPane.showMessageDialog(null, "Wrong type for grade! Input again");
this.dispose();
AddStudent.GUI();
}
}else{
JOptionPane.showMessageDialog(null, "File is not exist, please input correct file in computer! ");
this.dispose();
AddStudent.GUI();
}
} else if (e.getSource().equals(btnCancel)) {
this.dispose();
Menu.GUI();
}
}
}