Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
- */
- package com.mycompany.signupform;
- /**
- *
- * @author Admin
- */
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.File;
- import javax.swing.filechooser.FileNameExtensionFilter;
- public class SignupForm {
- public static void main(String[] args) {
- JFrame frame = new JFrame("Sign-Up Creation");
- frame.setSize(950, 650);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLayout(new BorderLayout());
- frame.getContentPane().setBackground(new Color(240, 240, 240));
- JPanel mainPanel = new JPanel();
- mainPanel.setLayout(null);
- mainPanel.setBackground(new Color(240, 240, 240));
- mainPanel.setPreferredSize(new Dimension(930, 800));
- JScrollPane scrollPane = new JScrollPane(mainPanel);
- scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
- frame.add(scrollPane, BorderLayout.CENTER);
- JLabel titleLabel = new JLabel("SIGN-UP CREATION");
- titleLabel.setFont(new Font("Arial", Font.BOLD, 20));
- titleLabel.setBounds(20, 10, 400, 35);
- mainPanel.add(titleLabel);
- String[] labels = {"LAST NAME:", "FIRST NAME:", "MIDDLE NAME:", "SUFFIX:", "SEX:", "SPECIALIZATION:", "WORK POSITION:", "EMAIL:", "CONTACT NO:"};
- JTextField[] textFields = new JTextField[labels.length];
- int yOffset = 70;
- for (int i = 0; i < labels.length; i++) {
- JLabel label = new JLabel(labels[i]);
- label.setFont(new Font("Arial", Font.BOLD, 11));
- label.setBounds(20, yOffset, 140, 25);
- mainPanel.add(label);
- if (labels[i].equals("SEX:")) {
- JComboBox<String> sexComboBox = new JComboBox<>(new String[]{"Male", "Female"});
- sexComboBox.setBounds(160, yOffset, 350, 25);
- mainPanel.add(sexComboBox);
- } else {
- textFields[i] = new JTextField();
- textFields[i].setBounds(160, yOffset, 350, 25);
- mainPanel.add(textFields[i]);
- }
- yOffset += 35;
- }
- // Restrict contact number to only digits and max 11 characters
- textFields[8].addKeyListener(new KeyAdapter() {
- public void keyTyped(KeyEvent e) {
- char c = e.getKeyChar();
- if (!Character.isDigit(c) || textFields[8].getText().length() >= 11) {
- e.consume();
- }
- }
- });
- JSeparator separator = new JSeparator();
- separator.setBounds(20, yOffset, 500, 10);
- mainPanel.add(separator);
- yOffset += 15;
- JLabel usernameLabel = new JLabel("USERNAME:");
- usernameLabel.setFont(new Font("Arial", Font.BOLD, 11));
- usernameLabel.setBounds(20, yOffset, 140, 25);
- mainPanel.add(usernameLabel);
- JTextField usernameField = new JTextField();
- usernameField.setBounds(160, yOffset, 350, 25);
- mainPanel.add(usernameField);
- JLabel usernameWarning = new JLabel("⚠");
- usernameWarning.setBounds(520, yOffset, 20, 25);
- usernameWarning.setForeground(Color.RED);
- usernameWarning.setVisible(false);
- mainPanel.add(usernameWarning);
- yOffset += 35;
- JLabel passwordLabel = new JLabel("PASSWORD:");
- passwordLabel.setFont(new Font("Arial", Font.BOLD, 11));
- passwordLabel.setBounds(20, yOffset, 140, 25);
- mainPanel.add(passwordLabel);
- JPasswordField passwordField = new JPasswordField();
- passwordField.setBounds(160, yOffset, 310, 25);
- mainPanel.add(passwordField);
- JButton togglePassword = new JButton("👁");
- togglePassword.setBounds(470, yOffset, 50, 25);
- mainPanel.add(togglePassword);
- JLabel passwordWarning = new JLabel("⚠");
- passwordWarning.setBounds(520, yOffset, 20, 25);
- passwordWarning.setForeground(Color.RED);
- passwordWarning.setVisible(false);
- mainPanel.add(passwordWarning);
- togglePassword.addActionListener(e -> {
- passwordField.setEchoChar(passwordField.getEchoChar() == '\u2022' ? (char) 0 : '\u2022');
- });
- yOffset += 35;
- JLabel confirmPasswordLabel = new JLabel("CONFIRM PASSWORD:");
- confirmPasswordLabel.setFont(new Font("Arial", Font.BOLD, 11));
- confirmPasswordLabel.setBounds(20, yOffset, 140, 25);
- mainPanel.add(confirmPasswordLabel);
- JPasswordField confirmPasswordField = new JPasswordField();
- confirmPasswordField.setBounds(160, yOffset, 310, 25);
- mainPanel.add(confirmPasswordField);
- JButton toggleConfirmPassword = new JButton("👁");
- toggleConfirmPassword.setBounds(470, yOffset, 50, 25);
- mainPanel.add(toggleConfirmPassword);
- JLabel confirmPasswordWarning = new JLabel("⚠");
- confirmPasswordWarning.setBounds(520, yOffset, 20, 25);
- confirmPasswordWarning.setForeground(Color.RED);
- confirmPasswordWarning.setVisible(false);
- mainPanel.add(confirmPasswordWarning);
- toggleConfirmPassword.addActionListener(e -> {
- confirmPasswordField.setEchoChar(confirmPasswordField.getEchoChar() == '\u2022' ? (char) 0 : '\u2022');
- });
- yOffset += 50;
- JButton submitButton = new JButton("SIGN UP");
- submitButton.setBounds(160, yOffset, 140, 35);
- submitButton.setFont(new Font("Arial", Font.BOLD, 11));
- submitButton.setBackground(Color.BLACK);
- submitButton.setForeground(Color.WHITE);
- submitButton.setEnabled(false);
- mainPanel.add(submitButton);
- JLabel pictureBox = new JLabel("Select Profile Picture", SwingConstants.CENTER);
- pictureBox.setBounds(600, 70, 150, 150);
- pictureBox.setBorder(BorderFactory.createLineBorder(Color.BLACK));
- mainPanel.add(pictureBox);
- pictureBox.addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent e) {
- JFileChooser fileChooser = new JFileChooser();
- fileChooser.setFileFilter(new FileNameExtensionFilter("Image Files", "jpg", "png", "jpeg"));
- int returnValue = fileChooser.showOpenDialog(null);
- if (returnValue == JFileChooser.APPROVE_OPTION) {
- File selectedFile = fileChooser.getSelectedFile();
- ImageIcon imageIcon = new ImageIcon(new ImageIcon(selectedFile.getAbsolutePath()).getImage().getScaledInstance(150, 150, Image.SCALE_SMOOTH));
- pictureBox.setIcon(imageIcon);
- pictureBox.setText("");
- }
- }
- });
- KeyAdapter validationListener = new KeyAdapter() {
- public void keyReleased(KeyEvent e) {
- boolean usernameValid = usernameField.getText().length() >= 6;
- boolean passwordValid = isStrongPassword(new String(passwordField.getPassword()));
- boolean passwordsMatch = new String(passwordField.getPassword()).equals(new String(confirmPasswordField.getPassword()));
- usernameWarning.setVisible(!usernameValid);
- passwordWarning.setVisible(!passwordValid);
- confirmPasswordWarning.setVisible(!passwordsMatch);
- submitButton.setEnabled(usernameValid && passwordValid && passwordsMatch);
- }
- };
- usernameField.addKeyListener(validationListener);
- passwordField.addKeyListener(validationListener);
- confirmPasswordField.addKeyListener(validationListener);
- submitButton.addActionListener(e -> JOptionPane.showMessageDialog(frame, "Signed Up Successfully!!!", "Success", JOptionPane.INFORMATION_MESSAGE));
- frame.setVisible(true);
- }
- private static boolean isStrongPassword(String password) {
- return password.length() >= 6 && password.matches(".*[A-Za-z].*") && password.matches(".*\\d.*") && password.matches(".*[^A-Za-z0-9].*");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement