-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistrationFormToFile.java
More file actions
147 lines (133 loc) · 6.41 KB
/
Copy pathRegistrationFormToFile.java
File metadata and controls
147 lines (133 loc) · 6.41 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
import javax.swing.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class RegistrationFormToFile {
public static void main(String[] args) {
JFrame frame = new JFrame("Registration Form to file");
frame.setSize(500, 500);
frame.setLayout(null);
JLabel usernameLabel = new JLabel("Username:");
usernameLabel.setBounds(10,10,100,20);
frame.add(usernameLabel);
JTextField usernameField = new JTextField();
usernameField.setBounds(100,10,200,20);
frame.add(usernameField);
JLabel emailLabel = new JLabel("Email:");
emailLabel.setBounds(10,40,100,20);
frame.add(emailLabel);
JTextField emailField = new JTextField();
emailField.setBounds(100,40,200,20);
frame.add(emailField);
JLabel phoneLabel = new JLabel("Phone:");
phoneLabel.setBounds(10,70,100,20);
frame.add(phoneLabel);
JTextField phoneField = new JTextField();
phoneField.setBounds(100,70,200,20);
frame.add(phoneField);
JLabel genderLabel = new JLabel("Gender:");
genderLabel.setBounds(10,100,100,20);
frame.add(genderLabel);
JRadioButton male = new JRadioButton("Male");
male.setBounds(100,100,70,20);
frame.add(male);
JRadioButton female = new JRadioButton("Female");
female.setBounds(170,100,70,20);
frame.add(female);
ButtonGroup genderGroup = new ButtonGroup();
genderGroup.add(male);
genderGroup.add(female);
JLabel countryLabel = new JLabel("Country:");
countryLabel.setBounds(10,130,100,20);
frame.add(countryLabel);
String[] countries = {"Select","Bangladesh","India","Pakistan","Afghanistan","Saudi Arabia","Palestine","Iran"};
JComboBox<String> countryComboBox = new JComboBox<>(countries);
countryComboBox.setBounds(100,130,100,20);
frame.add(countryComboBox);
JLabel DOBLabel = new JLabel("Date of Birth:");
DOBLabel.setBounds(10,160,100,20);
frame.add(DOBLabel);
String[] days = new String[31];
for (int i = 1; i <= 31; i++) {
days[i-1] = String.valueOf(i);
}
String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
String[] years = new String[100];
for (int i = 0; i < 100; i++) {
years[i] = String.valueOf(2025-i);
}
JComboBox<String> dayComboBox = new JComboBox<>(days);
dayComboBox.setBounds(100,160,50,20);
JComboBox<String > monthComboBox = new JComboBox<>(months);
monthComboBox.setBounds(150,160,50,20);
JComboBox<String> yearComboBox = new JComboBox<>(years);
yearComboBox.setBounds(200,160,60,20);
frame.add(dayComboBox);
frame.add(monthComboBox);
frame.add(yearComboBox);
JLabel fileLabel = new JLabel("Profile Picture:");
fileLabel.setBounds(10,190,100,20);
frame.add(fileLabel);
JButton uploadButton = new JButton("Choose file");
uploadButton.setBounds(100,190,100,20);
frame.add(uploadButton);
JLabel filePathLabel = new JLabel("");
filePathLabel.setBounds(100,220,300,20);
frame.add(filePathLabel);
final String[] selectedFilePath = {""};
uploadButton.addActionListener(e -> {
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(frame);
if(returnValue == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
selectedFilePath[0] = file.getAbsolutePath();
filePathLabel.setText(file.getName());
}
});
JButton submitButton = new JButton("Submit");
submitButton.setBounds(100,250,80,20);
frame.add(submitButton);
submitButton.addActionListener(e -> {
String username = usernameField.getText();
String email = emailField.getText();
String phone = String.valueOf(phoneField.getText());
String gender = male.isSelected() ? "Male" : female.isSelected() ? "Female" : "Not Selected";
String country = (String) countryComboBox.getSelectedItem();
String dateOfBirth = dayComboBox.getSelectedItem()+" "+monthComboBox.getSelectedItem()+","+yearComboBox.getSelectedItem();
String photo = selectedFilePath[0].isEmpty() ? "Not uploaded" : selectedFilePath[0];
if(username.isEmpty() || email.isEmpty() || phone.isEmpty() || gender.equals("Not Selected") || country.equals("Select") || photo.equals("Not uploaded") ) {
JOptionPane.showMessageDialog(frame, "Please fill all the fields","Error",JOptionPane.ERROR_MESSAGE);
} else {
try {
FileWriter writer = new FileWriter("registration_data.txt",true);
writer.write("Username: " + username
+ "\nEmail: " + email
+ "\nPhone: " + phone
+ "\nGender: " + gender
+ "\nCountry: " + country
+ "\nDate of Birth: " + dateOfBirth
+ "\nPhoto: " + photo
+ "\n--------------------------------------------------------------------------\n"
);
writer.close();
JOptionPane.showMessageDialog(frame, "Registration Saved Successfully!", "Success", JOptionPane.INFORMATION_MESSAGE);
usernameField.setText("");
emailField.setText("");
phoneField.setText("");
genderGroup.clearSelection();
countryComboBox.setSelectedIndex(0);
dayComboBox.setSelectedIndex(0);
monthComboBox.setSelectedIndex(0);
yearComboBox.setSelectedIndex(0);
filePathLabel.setText("");
selectedFilePath[0] = "";
} catch (IOException exception) {
JOptionPane.showMessageDialog(frame, "Error writing to file!", "Error", JOptionPane.ERROR_MESSAGE);
exception.printStackTrace();
}
}
});
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}