NewJFrame.java Source code

Java tutorial

Introduction

Here is the source code for NewJFrame.java

Source

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.UnknownHostException;

import com.mongodb.*;

/*
 * 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.
 */

/**
 * 
 * @author yogesh
 */
public class NewJFrame extends javax.swing.JFrame implements ActionListener {

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        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", "rawtypes" })
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jPassword1 = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        /*
         * Border border=BorderFactory.createRaisedSoftBevelBorder();
         * jLabel1.setBorder(border);
         */

        jLabel1.setFont(new java.awt.Font("SansSerif", 3, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(204, 255, 204));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Gym Managment");
        jLabel1.setBounds(130, 10, 210, 60);
        add(jLabel1);

        jLabel2.setFont(new java.awt.Font("SansSerif", 3, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(204, 255, 204));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("USER TYPE :");
        add(jLabel2);
        jLabel2.setBounds(30, 203, 160, 50);
        jLabel2.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jLabel2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Admin", "Trainer" }));
        jComboBox1.setToolTipText("");
        jComboBox1.addActionListener(this);
        add(jComboBox1);
        jComboBox1.setBounds(220, 213, 110, 30);

        jLabel3.setFont(new java.awt.Font("SansSerif", 3, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(204, 255, 204));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("User Name:");
        jLabel3.setBounds(30, 83, 160, 50);
        add(jLabel3);

        jTextField1.setBounds(220, 92, 160, 30);
        add(jTextField1);

        jLabel4.setFont(new java.awt.Font("SansSerif", 3, 12)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(204, 255, 204));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setBounds(30, 143, 160, 50);
        add(jLabel4);
        jLabel4.setText("PassWord:");

        jPassword1.setBounds(220, 153, 160, 30);
        add(jPassword1);

        jButton1.setText("Sign In");
        jButton1.setBackground(new java.awt.Color(51, 0, 51));
        jButton1.setForeground(new java.awt.Color(255, 255, 255));
        jButton1.setBounds(146, 290, 162, 30);
        jButton1.addActionListener(this);
        add(jButton1);

        setLayout(null);
        getContentPane().setBackground(new java.awt.Color(88, 0, 0));
        setSize(520, 400);
        setTitle("Login Page");
    }

    /**
     * @param args
     *            the command line arguments
     */
    public static void main(String args[]) {
        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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        // </editor-fold>

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

    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JPasswordField jPassword1;

    // End of variables declaration
    @Override
    public void actionPerformed(ActionEvent ae) {
        // TODO Auto-generated method stub
        add(jLabel5);
        String user = jTextField1.getText().toString();
        @SuppressWarnings("deprecation")
        String pass = jPassword1.getText().toString();
        jLabel5.setText("");

        if (ae.getSource() == jButton1) {
            if ((String) jComboBox1.getSelectedItem() == "Admin") {
                if (user.equals("admin") && pass.equals("admin123")) {
                    this.setVisible(false);
                    new Admin().setVisible(true);
                } else {
                    jLabel5.setFont(new java.awt.Font("SansSerif", 3, 14)); // NOI18N
                    jLabel5.setForeground(new java.awt.Color(25, 25, 112));
                    jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
                    jLabel5.setText("Login Not Successful ");
                    jTextField1.setText("");
                    jPassword1.setText("");
                    jLabel5.setBounds(120, 245, 170, 50);
                }
            } else if ((String) jComboBox1.getSelectedItem() == "Trainer") {
                try {
                    MongoClient mongo = new MongoClient("localhost", 27017);
                    DB db = mongo.getDB("Gym");
                    DBCollection Gym_Collection = db.getCollection("trainer");
                    BasicDBObject fields = new BasicDBObject("Username", user).append("Password", pass);
                    String s;
                    DBCursor cursor1 = Gym_Collection.find(fields);
                    if (cursor1.hasNext()) {
                        cursor1.next();
                        s = cursor1.curr().get("First_Name").toString();
                        new Receptionist(s);
                        this.setVisible(false);
                        new Receptionist().setVisible(true);
                    } else {
                        jLabel5.setFont(new java.awt.Font("SansSerif", 3, 14)); // NOI18N
                        jLabel5.setForeground(new java.awt.Color(25, 25, 112));
                        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
                        jLabel5.setText("Login Not Successful ");
                        jTextField1.setText("");
                        jPassword1.setText("");
                        jLabel5.setBounds(120, 245, 170, 50);
                    }
                } catch (UnknownHostException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }
    }
}