-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI_Enrollment_Add.java
More file actions
133 lines (97 loc) · 3.29 KB
/
GUI_Enrollment_Add.java
File metadata and controls
133 lines (97 loc) · 3.29 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.*;
import javax.swing.table.*;
public class GUI_Enrollment_Add extends JFrame {
static DefaultTableModel model;
static JTable table;
static JScrollPane scrollPane;
public static void main(String[] args) {
new GUI_Enrollment_Add();
}
GUI_Enrollment_Add(){
JButton btnNewButton = new JButton("Enroll Student");
btnNewButton.setBounds(86, 402, 114, 39);
add(btnNewButton);
JLabel lblCategory = new JLabel("Address");
lblCategory.setBounds(48, 116, 175, 19);
add(lblCategory);
JTextField txtName = new JTextField();
txtName.setBounds(48, 78, 188, 27);
add(txtName);
txtName.setColumns(10);
JTextField txtAddress = new JTextField();
txtAddress.setColumns(10);
txtAddress.setBounds(48, 146, 188, 27);
add(txtAddress);
JTextField txtCourse = new JTextField();
txtCourse.setColumns(10);
txtCourse.setBounds(48, 211, 188, 27);
add(txtCourse);
JLabel lblCategory_1_1 = new JLabel("Student Name");
lblCategory_1_1.setBounds(47, 53, 175, 19);
add(lblCategory_1_1);
JLabel lblTotalBill = new JLabel("Course");
lblTotalBill.setBounds(48, 181, 175, 19);
add(lblTotalBill);
JLabel lblAge = new JLabel("Age");
lblAge.setBounds(48, 249, 175, 19);
add(lblAge);
JTextField txtAge = new JTextField();
txtAge.setColumns(10);
txtAge.setBounds(48, 279, 188, 27);
add(txtAge);
JLabel lblTotalBill_1 = new JLabel("Last School Attended");
lblTotalBill_1.setBounds(48, 324, 175, 19);
add(lblTotalBill_1);
JTextField txtSchool = new JTextField();
txtSchool.setColumns(10);
txtSchool.setBounds(48, 354, 188, 27);
add(txtSchool);
String columns[] = {"Name","Address", "Course", "Age", "Last School"};
model = new DefaultTableModel(columns,0);
table = new JTable(model);
scrollPane = new JScrollPane(table);
add(scrollPane).setBounds(250,50,400,300);
readFromFile();
btnNewButton.addActionListener(e-> {
String name = txtName.getText();
String address = txtAddress.getText();
String course = txtCourse.getText();
String age = txtAge.getText();
String school = txtSchool.getText();
try {
savetoFile(name, address, course, age, school);
} catch (Exception ex) {
ex.printStackTrace();
}
readFromFile();
});
setSize(700,500);
setTitle("Enrollment");
setLayout(null);
setUndecorated(false);
setResizable(true);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void savetoFile(String name, String address, String course, String age, String school) throws Exception {
FileWriter fw = new FileWriter("EnrolledStudent.txt", true);
fw.write(name + "#" + address + "#" + course + "#" + age + "#" + school + "\n");
fw.close();
}
public static void readFromFile(){
model.setRowCount(0);
try(BufferedReader br = new BufferedReader(new FileReader("EnrolledStudent.txt"))){
String line;
while((line = br.readLine()) !=null){
String row[] = line.split("#");
model.addRow(row);
}
}catch(IOException e){
}
}
}