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