· 8 months ago · Mar 10, 2025, 06:20 AM
1/*
2 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
3 */
4
5package com.mycompany.signupform;
6
7/**
8 *
9 * @author Admin
10 */
11import javax.swing.*;
12import java.awt.*;
13import java.awt.event.*;
14import java.io.File;
15import javax.swing.filechooser.FileNameExtensionFilter;
16
17public class SignupForm {
18 public static void main(String[] args) {
19 JFrame frame = new JFrame("Sign-Up Creation");
20 frame.setSize(950, 650);
21 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
22 frame.setLayout(new BorderLayout());
23 frame.getContentPane().setBackground(new Color(240, 240, 240));
24
25 JPanel mainPanel = new JPanel();
26 mainPanel.setLayout(null);
27 mainPanel.setBackground(new Color(240, 240, 240));
28 mainPanel.setPreferredSize(new Dimension(930, 800));
29
30 JScrollPane scrollPane = new JScrollPane(mainPanel);
31 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
32 frame.add(scrollPane, BorderLayout.CENTER);
33
34 JLabel titleLabel = new JLabel("SIGN-UP CREATION");
35 titleLabel.setFont(new Font("Arial", Font.BOLD, 20));
36 titleLabel.setBounds(20, 10, 400, 35);
37 mainPanel.add(titleLabel);
38
39 String[] labels = {"LAST NAME:", "FIRST NAME:", "MIDDLE NAME:", "SUFFIX:", "SEX:", "SPECIALIZATION:", "WORK POSITION:", "EMAIL:", "CONTACT NO:"};
40 JTextField[] textFields = new JTextField[labels.length];
41
42 int yOffset = 70;
43 for (int i = 0; i < labels.length; i++) {
44 JLabel label = new JLabel(labels[i]);
45 label.setFont(new Font("Arial", Font.BOLD, 11));
46 label.setBounds(20, yOffset, 140, 25);
47 mainPanel.add(label);
48
49 if (labels[i].equals("SEX:")) {
50 JComboBox<String> sexComboBox = new JComboBox<>(new String[]{"Male", "Female"});
51 sexComboBox.setBounds(160, yOffset, 350, 25);
52 mainPanel.add(sexComboBox);
53 } else {
54 textFields[i] = new JTextField();
55 textFields[i].setBounds(160, yOffset, 350, 25);
56 mainPanel.add(textFields[i]);
57 }
58
59 yOffset += 35;
60 }
61
62 // Restrict contact number to only digits and max 11 characters
63 textFields[8].addKeyListener(new KeyAdapter() {
64 public void keyTyped(KeyEvent e) {
65 char c = e.getKeyChar();
66 if (!Character.isDigit(c) || textFields[8].getText().length() >= 11) {
67 e.consume();
68 }
69 }
70 });
71
72 JSeparator separator = new JSeparator();
73 separator.setBounds(20, yOffset, 500, 10);
74 mainPanel.add(separator);
75 yOffset += 15;
76
77 JLabel usernameLabel = new JLabel("USERNAME:");
78 usernameLabel.setFont(new Font("Arial", Font.BOLD, 11));
79 usernameLabel.setBounds(20, yOffset, 140, 25);
80 mainPanel.add(usernameLabel);
81
82 JTextField usernameField = new JTextField();
83 usernameField.setBounds(160, yOffset, 350, 25);
84 mainPanel.add(usernameField);
85
86 JLabel usernameWarning = new JLabel("⚠");
87 usernameWarning.setBounds(520, yOffset, 20, 25);
88 usernameWarning.setForeground(Color.RED);
89 usernameWarning.setVisible(false);
90 mainPanel.add(usernameWarning);
91
92 yOffset += 35;
93
94 JLabel passwordLabel = new JLabel("PASSWORD:");
95 passwordLabel.setFont(new Font("Arial", Font.BOLD, 11));
96 passwordLabel.setBounds(20, yOffset, 140, 25);
97 mainPanel.add(passwordLabel);
98
99 JPasswordField passwordField = new JPasswordField();
100 passwordField.setBounds(160, yOffset, 310, 25);
101 mainPanel.add(passwordField);
102
103 JButton togglePassword = new JButton("👁");
104 togglePassword.setBounds(470, yOffset, 50, 25);
105 mainPanel.add(togglePassword);
106
107 JLabel passwordWarning = new JLabel("⚠");
108 passwordWarning.setBounds(520, yOffset, 20, 25);
109 passwordWarning.setForeground(Color.RED);
110 passwordWarning.setVisible(false);
111 mainPanel.add(passwordWarning);
112
113 togglePassword.addActionListener(e -> {
114 passwordField.setEchoChar(passwordField.getEchoChar() == '\u2022' ? (char) 0 : '\u2022');
115 });
116
117 yOffset += 35;
118
119 JLabel confirmPasswordLabel = new JLabel("CONFIRM PASSWORD:");
120 confirmPasswordLabel.setFont(new Font("Arial", Font.BOLD, 11));
121 confirmPasswordLabel.setBounds(20, yOffset, 140, 25);
122 mainPanel.add(confirmPasswordLabel);
123
124 JPasswordField confirmPasswordField = new JPasswordField();
125 confirmPasswordField.setBounds(160, yOffset, 310, 25);
126 mainPanel.add(confirmPasswordField);
127
128 JButton toggleConfirmPassword = new JButton("👁");
129 toggleConfirmPassword.setBounds(470, yOffset, 50, 25);
130 mainPanel.add(toggleConfirmPassword);
131
132 JLabel confirmPasswordWarning = new JLabel("⚠");
133 confirmPasswordWarning.setBounds(520, yOffset, 20, 25);
134 confirmPasswordWarning.setForeground(Color.RED);
135 confirmPasswordWarning.setVisible(false);
136 mainPanel.add(confirmPasswordWarning);
137
138 toggleConfirmPassword.addActionListener(e -> {
139 confirmPasswordField.setEchoChar(confirmPasswordField.getEchoChar() == '\u2022' ? (char) 0 : '\u2022');
140 });
141
142 yOffset += 50;
143
144 JButton submitButton = new JButton("SIGN UP");
145 submitButton.setBounds(160, yOffset, 140, 35);
146 submitButton.setFont(new Font("Arial", Font.BOLD, 11));
147 submitButton.setBackground(Color.BLACK);
148 submitButton.setForeground(Color.WHITE);
149 submitButton.setEnabled(false);
150 mainPanel.add(submitButton);
151
152 JLabel pictureBox = new JLabel("Select Profile Picture", SwingConstants.CENTER);
153 pictureBox.setBounds(600, 70, 150, 150);
154 pictureBox.setBorder(BorderFactory.createLineBorder(Color.BLACK));
155 mainPanel.add(pictureBox);
156
157 pictureBox.addMouseListener(new MouseAdapter() {
158 public void mouseClicked(MouseEvent e) {
159 JFileChooser fileChooser = new JFileChooser();
160 fileChooser.setFileFilter(new FileNameExtensionFilter("Image Files", "jpg", "png", "jpeg"));
161 int returnValue = fileChooser.showOpenDialog(null);
162 if (returnValue == JFileChooser.APPROVE_OPTION) {
163 File selectedFile = fileChooser.getSelectedFile();
164 ImageIcon imageIcon = new ImageIcon(new ImageIcon(selectedFile.getAbsolutePath()).getImage().getScaledInstance(150, 150, Image.SCALE_SMOOTH));
165 pictureBox.setIcon(imageIcon);
166 pictureBox.setText("");
167 }
168 }
169 });
170
171 KeyAdapter validationListener = new KeyAdapter() {
172 public void keyReleased(KeyEvent e) {
173 boolean usernameValid = usernameField.getText().length() >= 6;
174 boolean passwordValid = isStrongPassword(new String(passwordField.getPassword()));
175 boolean passwordsMatch = new String(passwordField.getPassword()).equals(new String(confirmPasswordField.getPassword()));
176
177 usernameWarning.setVisible(!usernameValid);
178 passwordWarning.setVisible(!passwordValid);
179 confirmPasswordWarning.setVisible(!passwordsMatch);
180
181 submitButton.setEnabled(usernameValid && passwordValid && passwordsMatch);
182 }
183 };
184
185 usernameField.addKeyListener(validationListener);
186 passwordField.addKeyListener(validationListener);
187 confirmPasswordField.addKeyListener(validationListener);
188
189 submitButton.addActionListener(e -> JOptionPane.showMessageDialog(frame, "Signed Up Successfully!!!", "Success", JOptionPane.INFORMATION_MESSAGE));
190
191 frame.setVisible(true);
192 }
193
194 private static boolean isStrongPassword(String password) {
195 return password.length() >= 6 && password.matches(".*[A-Za-z].*") && password.matches(".*\\d.*") && password.matches(".*[^A-Za-z0-9].*");
196 }
197}