payroll.FrmPassword.java Source code

Java tutorial

Introduction

Here is the source code for payroll.FrmPassword.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package payroll;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import java.awt.Color;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Gopinath
 */
public class FrmPassword extends javax.swing.JFrame {

    /**
     * Creates new form User
     */

    public FrmPassword() {
        initComponents();
        this.getContentPane().setBackground(Color.YELLOW);
        setDefaultCloseOperation(this.HIDE_ON_CLOSE);

    }

    /**
     * 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() {

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        lblPwd = new javax.swing.JLabel();
        lblEmpid = new javax.swing.JLabel();
        lblEmpId = new javax.swing.JLabel();
        lblRetypepwd = new javax.swing.JLabel();
        pwdPassword = new javax.swing.JPasswordField();
        btnSubmit = new javax.swing.JButton();
        pwdRetype = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Set a Password");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jPanel2.setBackground(new java.awt.Color(255, 255, 0));

        jPanel1.setBackground(new java.awt.Color(153, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED, null, null, null,
                        java.awt.Color.blue),
                "Set Password", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
                javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Algerian", 2, 12),
                new java.awt.Color(153, 51, 0))); // NOI18N

        lblPwd.setBackground(new java.awt.Color(255, 255, 255));
        lblPwd.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        lblPwd.setText("Password:");

        lblEmpid.setBackground(new java.awt.Color(255, 255, 255));
        lblEmpid.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        lblEmpid.setText("Employee Id:");

        lblEmpId.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N

        lblRetypepwd.setBackground(new java.awt.Color(255, 255, 255));
        lblRetypepwd.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        lblRetypepwd.setText("Re-type Password:");

        pwdPassword.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N

        btnSubmit.setBackground(new java.awt.Color(255, 255, 255));
        btnSubmit.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N
        btnSubmit.setText("Submit");
        btnSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubmitActionPerformed(evt);
            }
        });

        pwdRetype.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(lblPwd).addComponent(lblEmpid).addComponent(lblRetypepwd))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(
                                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addComponent(btnSubmit).addComponent(pwdPassword).addComponent(pwdRetype)
                                        .addComponent(lblEmpId, javax.swing.GroupLayout.PREFERRED_SIZE, 89,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lblEmpid).addComponent(lblEmpId,
                                        javax.swing.GroupLayout.PREFERRED_SIZE, 20,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblPwd).addComponent(pwdPassword,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblRetypepwd).addComponent(pwdRetype,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(38, 38, 38).addComponent(btnSubmit).addContainerGap()));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addGap(78, 78, 78)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(96, Short.MAX_VALUE)));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addGap(41, 41, 41)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(55, Short.MAX_VALUE)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel2,
                        javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed
        // TODO add your handling code here:
        int id = 0;
        Connect1 j = new Connect1();
        DBCollection dbc = j.connect("Passwords");
        BasicDBObject bdb = new BasicDBObject();
        final JPanel p2 = new JPanel();
        final JOptionPane jo = new JOptionPane();
        Connect1 k = new Connect1();
        DBCollection dbc1 = k.connect("Emp_Records");
        DBCursor cursor = dbc1.find();
        while (cursor.hasNext() == true) {
            DBObject obj = cursor.next();
            id++;
        }
        String ID = "";

        id = id - 1;

        if (id < 10)
            ID = "EMP00" + id;
        else
            ID = "EMP0" + id;

        if (pwdRetype.getText().equals(pwdPassword.getText())) {
            bdb.put("Employee Id", ID);
            bdb.put("Password", pwdPassword.getText());
            dbc.insert(bdb);
            JOptionPane.showMessageDialog(p2, "Password Created Successfully", "Success",
                    JOptionPane.INFORMATION_MESSAGE);
            this.setVisible(false);

        } else {
            JOptionPane.showMessageDialog(p2, "Passwords do not match", "Failed", JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_btnSubmitActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        // TODO add your handling code here:
        int id = 0;
        Connect1 k = new Connect1();
        DBCollection dbc1 = k.connect("Emp_Records");
        DBCursor cursor = dbc1.find();
        while (cursor.hasNext() == true) {
            DBObject obj = cursor.next();
            id++;
        }
        id--;
        String ID = "";
        if (id < 10)
            ID = "EMP00" + id;
        else
            ID = "EMP0" + id;
        ID.trim();
        lblEmpId.setText(ID);
    }//GEN-LAST:event_formWindowActivated

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmPassword.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmPassword.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmPassword.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmPassword.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmPassword().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSubmit;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblEmpId;
    private javax.swing.JLabel lblEmpid;
    private javax.swing.JLabel lblPwd;
    private javax.swing.JLabel lblRetypepwd;
    private javax.swing.JPasswordField pwdPassword;
    private javax.swing.JPasswordField pwdRetype;
    // End of variables declaration//GEN-END:variables
}