private void jButton1ActionPerformed( java.awt.event.ActionEvent evt) { // GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: if (NameField.getText().equals("")) { Message.setText("Fill Your Name field"); } else if (UsernameField.getText().equals("") || UsernameField.getText().contains(" ")) { Message.setText("Enter a User Name (without spaces)"); } else if (new String(jPasswordField1.getPassword()).equals("")) { Message.setText(" Enter a Password to login with."); } else if (!new String(jPasswordField1.getPassword()) .equals(new String(jPasswordField2.getPassword()))) { Message.setText("Passwords didn't match."); } else if (newUser()) { Projects p = new Projects(); this.setVisible(false); p.setVisible(true); } } // GEN-LAST:event_jButton1ActionPerformed
public boolean newUser() { FileWriter writer; try { if (find()) { Message.setText("This username is already registered"); return false; } writer = new FileWriter( "D:\\College\\2nd year\\2nd semester\\Test\\Java Project\\spreadSheet.txt"); writer.write(UsernameField.getText() + " " + new String(jPasswordField1.getPassword())); } catch (Exception e) { System.out.println(e.toString()); } return true; }
public boolean find() throws Exception { FileReader sheet = new FileReader("D:\\College\\2nd year\\2nd semester\\Test\\Java Project\\spreadSheet.txt"); BufferedReader br = new BufferedReader(sheet); String username = UsernameField.getText(), line; while (true) { line = br.readLine(); if (line == null) { break; } String[] words = line.split(" "); if (words[0].equalsIgnoreCase(username)) { return true; } } return false; }
/** * This method is called from within the constructor to initialize the form. WARNING: Do NOT * modify this code. The content of this method is always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); NameField = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); jPasswordField1 = new javax.swing.JPasswordField(); UsernameField = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); jPasswordField2 = new javax.swing.JPasswordField(); jLabel5 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); Message = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Sign Up"); setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N jLabel1.setFont(new java.awt.Font("Felix Titling", 0, 18)); // NOI18N jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel1.setText("Sign up Here!"); jLabel2.setFont(new java.awt.Font("Eras Demi ITC", 0, 11)); // NOI18N jLabel2.setText("Your Name"); NameField.setToolTipText("Enter your complete name here!"); jLabel3.setFont(new java.awt.Font("Eras Demi ITC", 0, 11)); // NOI18N jLabel3.setText("Password"); jPasswordField1.setToolTipText("Enter your Password here!"); UsernameField.setToolTipText("Enter your user name here!"); jLabel4.setFont(new java.awt.Font("Eras Demi ITC", 0, 11)); // NOI18N jLabel4.setText("User Name"); jPasswordField2.setToolTipText("Verify your Password !"); jLabel5.setFont(new java.awt.Font("Eras Demi ITC", 0, 11)); // NOI18N jLabel5.setText("Verify Password"); jButton1.setFont(new java.awt.Font("Eras Demi ITC", 0, 11)); // NOI18N jButton1.setText("Submit"); jButton1.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); Message.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N Message.setForeground(new java.awt.Color(255, 0, 0)); Message.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent( jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout .createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup( layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel5) .addComponent( jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent( UsernameField, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4) .addComponent( NameField, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2) .addComponent(jLabel3) .addComponent( jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(90, 90, 90)) .addGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( layout .createSequentialGroup() .addGap(128, 128, 128) .addComponent( jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup( layout .createSequentialGroup() .addGap(25, 25, 25) .addComponent( Message, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(37, Short.MAX_VALUE))); layout.setVerticalGroup( layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( layout .createSequentialGroup() .addGap(35, 35, 35) .addComponent(jLabel1) .addGap(18, 18, 18) .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent( NameField, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jLabel4) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent( UsernameField, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jLabel3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent( jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jLabel5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent( jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(31, 31, 31) .addComponent( jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent( Message, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(35, Short.MAX_VALUE))); pack(); } // </editor-fold>//GEN-END:initComponents
/** * This method is called from within the constructor to initialize the form. WARNING: Do NOT * modify this code. The content of this method is always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { ConfirmButton = new javax.swing.JButton(); CancelButton = new javax.swing.JButton(); RegisterUserLabel = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); UsernameField = new javax.swing.JTextField(); PasswordField = new javax.swing.JPasswordField(); ConfirmPasswordField = new javax.swing.JPasswordField(); EmailField = new javax.swing.JTextField(); ConfirmEmailField = new javax.swing.JTextField(); ConfirmEmailLabel = new javax.swing.JLabel(); EmailLabel = new javax.swing.JLabel(); ConfimPasswordLabel = new javax.swing.JLabel(); UsernameLabel = new javax.swing.JLabel(); PasswordLabel = new javax.swing.JLabel(); xEmail = new javax.swing.JLabel(); xPassword = new javax.swing.JLabel(); xName = new javax.swing.JLabel(); ErrorLabel = new javax.swing.JLabel(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("resources/Portugues_pt_PT_EURO"); // NOI18N setTitle(bundle.getString("AppTitle")); // NOI18N setResizable(false); getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout()); ConfirmButton.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N ConfirmButton.setText(bundle.getString("ConfirmButton")); // NOI18N ConfirmButton.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ConfirmButtonActionPerformed(evt); } }); getContentPane() .add(ConfirmButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(246, 444, 171, 55)); CancelButton.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N CancelButton.setText(bundle.getString("CancelButton")); // NOI18N CancelButton.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { CancelButtonActionPerformed(evt); } }); getContentPane() .add(CancelButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(454, 444, 165, 55)); RegisterUserLabel.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N RegisterUserLabel.setText(bundle.getString("RegisterUserLabel")); // NOI18N getContentPane() .add(RegisterUserLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(318, 60, -1, -1)); UsernameField.setAlignmentY(0.0F); UsernameField.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); PasswordField.setAlignmentY(0.0F); PasswordField.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); PasswordField.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { PasswordFieldActionPerformed(evt); } }); ConfirmPasswordField.setAlignmentY(0.0F); ConfirmPasswordField.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); EmailField.setAlignmentY(0.0F); EmailField.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); ConfirmEmailField.setAlignmentY(0.0F); ConfirmEmailField.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); ConfirmEmailLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N ConfirmEmailLabel.setText(bundle.getString("ConfirmEmailLabel")); // NOI18N ConfirmEmailLabel.setAlignmentY(0.0F); EmailLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N EmailLabel.setText("E-Mail:"); EmailLabel.setAlignmentY(0.0F); ConfimPasswordLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N ConfimPasswordLabel.setText(bundle.getString("ConfirmPasswordLabel")); // NOI18N ConfimPasswordLabel.setAlignmentY(0.0F); UsernameLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N UsernameLabel.setText(bundle.getString("UsernameLabel")); // NOI18N UsernameLabel.setAlignmentY(0.0F); UsernameLabel.addMouseListener( new java.awt.event.MouseAdapter() { public void mouseEntered(java.awt.event.MouseEvent evt) { UsernameLabelMouseEntered(evt); } }); PasswordLabel.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N PasswordLabel.setText("Password:"******"/resources/redcross.png"))); // NOI18N xPassword.setIcon( new javax.swing.ImageIcon(getClass().getResource("/resources/redcross.png"))); // NOI18N xName.setIcon( new javax.swing.ImageIcon(getClass().getResource("/resources/redcross.png"))); // NOI18N ErrorLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N ErrorLabel.setText("ERROR TEXT FIELD HERE..."); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout .createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( jPanel1Layout .createSequentialGroup() .addGroup( jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(ConfirmEmailLabel) .addComponent(EmailLabel) .addComponent(ConfimPasswordLabel)) .addGap(17, 17, 17)) .addGroup( javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout .createSequentialGroup() .addGroup( jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent( UsernameLabel, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent( PasswordLabel, javax.swing.GroupLayout.Alignment.TRAILING)) .addGap(18, 18, 18))) .addGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( jPanel1Layout .createSequentialGroup() .addComponent( PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(xPassword)) .addGroup( jPanel1Layout .createSequentialGroup() .addComponent( UsernameField, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(xName)) .addComponent( ConfirmPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup( jPanel1Layout .createSequentialGroup() .addComponent( EmailField, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(xEmail)) .addGroup( jPanel1Layout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent( ErrorLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent( ConfirmEmailField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE))))); jPanel1Layout.setVerticalGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( jPanel1Layout .createSequentialGroup() .addGap(32, 32, 32) .addGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent( UsernameField, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(UsernameLabel)) .addComponent( xName, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent( PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(PasswordLabel)) .addComponent(xPassword)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent( ConfirmPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(ConfimPasswordLabel)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent( EmailField, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(EmailLabel)) .addComponent( xEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup( jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent( ConfirmEmailField, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(ConfirmEmailLabel)) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE) .addComponent(ErrorLabel) .addContainerGap())); getContentPane() .add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(114, 122, -1, -1)); jLabel1.setIcon( new javax.swing.ImageIcon(getClass().getResource("/resources/background.jpg"))); // NOI18N getContentPane() .add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 860, 550)); pack(); } // </editor-fold>//GEN-END:initComponents
private void ConfirmButtonActionPerformed( java.awt.event.ActionEvent evt) { // GEN-FIRST:event_ConfirmButtonActionPerformed String username = UsernameField.getText(); char[] password = PasswordField.getPassword(); char[] passwordConfirm = ConfirmPasswordField.getPassword(); String email = EmailField.getText(); String emailConfirm = ConfirmEmailField.getText(); String pass = new String(password); String pConfirm = new String(passwordConfirm); MD5Pwd enc = new MD5Pwd(); String passEnc; xEmail.setVisible(false); xName.setVisible(false); xPassword.setVisible(false); passEnc = enc.encode(username, pass); if (ValidateMail.isValidEmailAddress(email)) { if (email.equals(emailConfirm)) { if (!"".equals(username)) { if (pass.equals(pConfirm) && (!"".equals(pass))) { try { ComCliente com = ComCliente.getInstance(); com.registar(username, passEnc, email); this.ConfirmButton.setEnabled(false); } catch (Exception e) { System.out.println("Accept failed: 4444"); System.exit(-1); } } else { this.ErrorLabel.setText( java.util.ResourceBundle.getBundle(Lang).getString("PasswordConfirmError")); this.xPassword.setVisible(true); this.PasswordField.setText(""); this.ConfirmPasswordField.setText(""); } } else { this.ErrorLabel.setText( java.util.ResourceBundle.getBundle(Lang).getString("InsertUsername")); this.xName.setVisible(true); } } else { this.ErrorLabel.setText( java.util.ResourceBundle.getBundle(Lang).getString("EmailConfirmationError")); this.EmailField.setText(""); this.ConfirmEmailField.setText(""); this.xEmail.setVisible(true); } } else { this.ErrorLabel.setText(java.util.ResourceBundle.getBundle(Lang).getString("InvalidEmail")); this.EmailField.setText(""); this.ConfirmEmailField.setText(""); this.xEmail.setVisible(true); } } // GEN-LAST:event_ConfirmButtonActionPerformed
private void addEnterListenerToUsernameField(Action action) { usernameField.getInputMap().put(ClientUI.ENTER, "login"); usernameField.getActionMap().put("login", action); }
private void addUsernameField() { usernameField = new UsernameField(); usernameField.setBounds(BOUNDS_USERNAME_FIELD); usernameField.setFont(ClientUI.FONT_NORMAL); contentPanel.add(usernameField); }