ABDULKARIM
ABDULKARIM
TASK: ASSIGNMENT 3
registration.
PART A: CODES
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public RegistrationForm() {
setTitle("Registration Form");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 350);
setLayout(new GridBagLayout());
1
titleLabel.setFont(new Font("Serif", Font.BOLD, 20));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 4;
gbc.anchor = GridBagConstraints.CENTER;
add(titleLabel, gbc);
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.LINE_END;
gbc.gridx = 0;
gbc.gridy = 1;
add(new JLabel("Full Name:"), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.anchor = GridBagConstraints.LINE_START;
add(fullNameField = new JTextField(30), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("Password:"), gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.LINE_START;
add(passwordField = new JPasswordField(15), gbc);
gbc.gridx = 2;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("Confirm Password:"), gbc);
gbc.gridx = 3;
gbc.gridy = 2;
gbc.anchor = GridBagConstraints.LINE_START;
add(confirmPasswordField = new JPasswordField(15), gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("Birth Month:"), gbc);
gbc.gridx = 1;
2
gbc.gridy = 3;
gbc.anchor = GridBagConstraints.LINE_START;
add(birthMonthCombo = new JComboBox<>(new String[]{"NotSelected",
"January", "February", "March", "April", "May", "June", "July", "August",
"September", "October", "November", "December"}), gbc);
gbc.gridx = 2;
gbc.gridy = 3;
gbc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("Birth Year:"), gbc);
gbc.gridx = 3;
gbc.gridy = 3;
gbc.anchor = GridBagConstraints.LINE_START;
add(birthYearField = new JTextField(6), gbc);
gbc.gridx = 0;
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("Gender:"), gbc);
gbc.gridx = 1;
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.LINE_START;
add(genderCombo = new JComboBox<>(new String[]{"NotSelected",
"Male", "Female"}), gbc);
gbc.gridx = 2;
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("Country:"), gbc);
gbc.gridx = 3;
gbc.gridy = 4;
gbc.anchor = GridBagConstraints.LINE_START;
add(countryCombo = new JComboBox<>(new String[]{"NotSelected",
"Kenya", "Tanzania", "Uganda", "Rwanda", "Burundi", "South Sudan"}),
gbc);
gbc.gridx = 0;
gbc.gridy = 5;
gbc.gridwidth = 1;
gbc.anchor = GridBagConstraints.CENTER;
add(saveButton = new JButton("Save"), gbc);
3
gbc.gridx = 1;
gbc.gridy = 5;
add(resetButton = new JButton("Reset"), gbc);
gbc.gridx = 2;
gbc.gridy = 5;
add(exitButton = new JButton("Exit"), gbc);
saveButton.addActionListener(new SaveButtonListener());
resetButton.addActionListener(e -> resetFields());
exitButton.addActionListener(e -> System.exit(0));
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String fullName = fullNameField.getText();
String password = new String(passwordField.getPassword());
String confirmPassword = new
String(confirmPasswordField.getPassword());
String birthYearStr = birthYearField.getText();
String birthMonth = (String)
birthMonthCombo.getSelectedItem();
String gender = (String) genderCombo.getSelectedItem();
String country = (String) countryCombo.getSelectedItem();
4
showError("Password must be at least 8 characters
long.");
return;
}
if (!password.equals(confirmPassword)) {
showError("Passwords do not match.");
return;
}
int birthYear;
try {
birthYear = Integer.parseInt(birthYearStr);
if (birthYear < 2005 || birthYear > 2010) {
showError("Birth Year must be between 2005 and
2010.");
return;
}
} catch (NumberFormatException ex) {
showError("Birth Year must be a valid number.");
return;
}
if (birthMonth.equals("NotSelected") ||
gender.equals("NotSelected") || country.equals("NotSelected")) {
showError("Please make valid selections in all combo
boxes.");
return;
}
5
}
PART B: OUTPUT
6
FILLING THE FORM
7
THE RESULTED OUTPUT
8
9