×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Israel Edet
Added: Jan 4, 2017 10:06 PM
Views: 12
Tags: no tags
GUI class for login
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package inventorysystem.gui;
  7.  
  8. import inventorysystem.data.Employee;
  9. import inventorysystem.data.Login;
  10. import javax.swing.JOptionPane;
  11. import javax.swing.JPanel;
  12. import inventorysystem.InventorySystem;
  13. import interfaces.Switchable;
  14. import java.util.logging.Logger;
  15.  
  16. /**
  17.  *
  18.  * @author Epic
  19.  */
  20. public class login extends javax.swing.JPanel implements Switchable {
  21. private static  boolean loggedIn = false;
  22. private static Employee currentEmployee = null;
  23.     /**
  24.      * Creates new form login
  25.      */
  26.     public login() {
  27.         initComponents();
  28.         this.setName("Login Panel");
  29.     }
  30.  
  31.     /**
  32.      * This method is called from within the constructor to initialize the form.
  33.      * WARNING: Do NOT modify this code. The content of this method is always
  34.      * regenerated by the Form Editor.
  35.      */
  36.     @SuppressWarnings("unchecked")
  37.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  38.     private void initComponents() {
  39.  
  40.         txtUsername = new javax.swing.JTextField();
  41.         txtPassword = new javax.swing.JPasswordField();
  42.         jLabel1 = new javax.swing.JLabel();
  43.         jLabel2 = new javax.swing.JLabel();
  44.         btnLogin = new javax.swing.JButton();
  45.         jLabel3 = new javax.swing.JLabel();
  46.         lblWrong = new javax.swing.JLabel();
  47.  
  48.         jLabel1.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
  49.         jLabel1.setText("Username:");
  50.  
  51.         jLabel2.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
  52.         jLabel2.setText("Password:");
  53.  
  54.         btnLogin.setFont(new java.awt.Font("Dialog", 1, 12)); // NOI18N
  55.         btnLogin.setText("Login");
  56.         btnLogin.addActionListener(new java.awt.event.ActionListener() {
  57.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  58.                 btnLoginActionPerformed(evt);
  59.             }
  60.         });
  61.  
  62.         jLabel3.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
  63.         jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  64.         jLabel3.setText("Inventory System for Group 1");
  65.  
  66.         lblWrong.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
  67.         lblWrong.setForeground(new java.awt.Color(255, 0, 0));
  68.         lblWrong.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
  69.         lblWrong.setText("Wrong user credentials");
  70.         lblWrong.setVisible(false);
  71.  
  72.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
  73.         this.setLayout(layout);
  74.         layout.setHorizontalGroup(
  75.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  76.             .addGroup(layout.createSequentialGroup()
  77.                 .addGap(30, 30, 30)
  78.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  79.                     .addGroup(layout.createSequentialGroup()
  80.                         .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
  81.                         .addContainerGap())
  82.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  83.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  84.                             .addComponent(lblWrong, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  85.                             .addComponent(btnLogin, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  86.                             .addComponent(txtUsername, javax.swing.GroupLayout.Alignment.LEADING)
  87.                             .addComponent(txtPassword, javax.swing.GroupLayout.Alignment.LEADING)
  88.                             .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
  89.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  90.                                     .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
  91.                                     .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
  92.                                 .addGap(0, 0, Short.MAX_VALUE)))
  93.                         .addGap(22, 22, 22))))
  94.         );
  95.         layout.setVerticalGroup(
  96.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  97.             .addGroup(layout.createSequentialGroup()
  98.                 .addGap(35, 35, 35)
  99.                 .addComponent(jLabel3)
  100.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  101.                 .addComponent(jLabel1)
  102.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  103.                 .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
  104.                 .addGap(18, 18, 18)
  105.                 .addComponent(jLabel2)
  106.                 .addGap(2, 2, 2)
  107.                 .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
  108.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
  109.                 .addComponent(lblWrong)
  110.                 .addGap(18, 18, 18)
  111.                 .addComponent(btnLogin)
  112.                 .addGap(32, 32, 32))
  113.         );
  114.     }// </editor-fold>                        
  115.  
  116.     private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {                                        
  117.         // TODO add your handling code here:
  118.         Login l = new Login();
  119.         this.currentEmployee = l.isUser(txtUsername.getText(), txtPassword.getText());
  120.        
  121.         if(null != this.currentEmployee){
  122.             lblWrong.setVisible(false);
  123.             JOptionPane.showMessageDialog(null, "Welcome ".concat(this.currentEmployee.getUsername()));
  124.             loggedIn = true;
  125.             //Goto new panel on the block
  126.             this.OnPanelSwitched(new Dashboard());
  127.         }else{
  128.             lblWrong.setVisible(true);
  129.         }
  130.     }                                        
  131.  
  132.     // <editor-fold defaultstate="collapsed" desc="Initialize variables">      
  133.     // Variables declaration - do not modify                    
  134.     private javax.swing.JButton btnLogin;
  135.     private javax.swing.JLabel jLabel1;
  136.     private javax.swing.JLabel jLabel2;
  137.     private javax.swing.JLabel jLabel3;
  138.     private javax.swing.JLabel lblWrong;
  139.     private javax.swing.JPasswordField txtPassword;
  140.     private javax.swing.JTextField txtUsername;
  141.     // End of variables declaration                  
  142. //</editor-fold>
  143.    
  144.     @Override
  145.     public void OnPanelSwitched(JPanel panelToSwitch) {
  146.         InventorySystem.switchPanel(panelToSwitch,loggedIn);
  147.         System.out.println("I have been called oh bro to change the panel "+panelToSwitch.getName());
  148.     }
  149.  
  150.     public static boolean isLoggedIn() {
  151.         return loggedIn;
  152.     }
  153.  
  154.     public  static Employee getCurrentEmployee() {
  155.         return currentEmployee;
  156.     }
  157.    
  158.     private static final Logger LOG = Logger.getLogger(login.class.getName());
  159.    
  160. }
  161.