C:\Users\Man\Desktop\IA_FinaL_Submission_000307-0092\Product\IA_Netbeans_Project\src\ia\man\Login.java
  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 ia.man;
  7 
  8 import java.awt.Dimension;
  9 import java.awt.Toolkit;
 10 import java.io.BufferedReader;
 11 import java.io.File;
 12 import java.io.FileReader;
 13 import java.io.IOException;
 14 import javax.swing.JOptionPane;
 15 
 16 /**
 17  *
 18  * @author 14868
 19  */
 20 public class Login extends javax.swing.JFrame {
 21 
 22     /**
 23      * Creates new form LoginRegister
 24      */
 25    
 26     public Login() {
 27         initComponents();
 28         Toolkit toolkit = getToolkit();
 29         Dimension size = toolkit.getScreenSize();
 30         setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2);
 31         //setDefaultCloseOperation(Register.EXIT_ON_CLOSE); //Close only this form
 32 
 33     }
 34 
 35     /**
 36      * This method is called from within the constructor to initialize the form.
 37      * WARNING: Do NOT modify this code. The content of this method is always
 38      * regenerated by the Form Editor.
 39      */
 40     @SuppressWarnings("unchecked")
 41     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
 42     private void initComponents() {
 43 
 44         jPanel2 = new javax.swing.JPanel();
 45         usernameLabel = new javax.swing.JLabel();
 46         passwordLabel = new javax.swing.JLabel();
 47         usernameTF = new javax.swing.JTextField();
 48         passwordTF = new javax.swing.JTextField();
 49         loginButton = new javax.swing.JToggleButton();
 50         registerButton = new javax.swing.JButton();
 51         registerLabel = new javax.swing.JLabel();
 52         jLabel1 = new javax.swing.JLabel();
 53 
 54         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 55         setBackground(new java.awt.Color(102, 204, 255));
 56 
 57         usernameLabel.setText("Username");
 58 
 59         passwordLabel.setText("Password");
 60 
 61         loginButton.setText("Login");
 62         loginButton.addMouseListener(new java.awt.event.MouseAdapter() {
 63             public void mouseClicked(java.awt.event.MouseEvent evt) {
 64                 loginButtonMouseClicked(evt);
 65             }
 66             public void mouseReleased(java.awt.event.MouseEvent evt) {
 67                 loginButtonMouseReleased(evt);
 68             }
 69         });
 70         loginButton.addActionListener(new java.awt.event.ActionListener() {
 71             public void actionPerformed(java.awt.event.ActionEvent evt) {
 72                 loginButtonActionPerformed(evt);
 73             }
 74         });
 75 
 76         registerButton.setText("Register");
 77         registerButton.addMouseListener(new java.awt.event.MouseAdapter() {
 78             public void mouseClicked(java.awt.event.MouseEvent evt) {
 79                 registerButtonMouseClicked(evt);
 80             }
 81         });
 82 
 83         registerLabel.setText("Don't have an account?");
 84 
 85         jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
 86         jLabel1.setText("Welcome!");
 87 
 88         javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
 89         jPanel2.setLayout(jPanel2Layout);
 90         jPanel2Layout.setHorizontalGroup(
 91             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 92             .addGroup(jPanel2Layout.createSequentialGroup()
 93                 .addGap(144, 144, 144)
 94                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
 95                     .addComponent(passwordLabel)
 96                     .addComponent(usernameLabel))
 97                 .addGap(18, 18, 18)
 98                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 99                     .addComponent(registerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
100                     .addComponent(loginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
101                     .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
102                         .addComponent(usernameTF)
103                         .addComponent(passwordTF, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE))
104                     .addComponent(registerLabel)
105                     .addGroup(jPanel2Layout.createSequentialGroup()
106                         .addGap(11, 11, 11)
107                         .addComponent(jLabel1)))
108                 .addContainerGap(185, Short.MAX_VALUE))
109         );
110         jPanel2Layout.setVerticalGroup(
111             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
112             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
113                 .addGap(26, 26, 26)
114                 .addComponent(jLabel1)
115                 .addGap(18, 18, 18)
116                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
117                     .addComponent(usernameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
118                     .addComponent(usernameTF, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
119                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
120                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
121                     .addComponent(passwordTF, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
122                     .addComponent(passwordLabel))
123                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
124                 .addComponent(loginButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
125                 .addGap(26, 26, 26)
126                 .addComponent(registerLabel)
127                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
128                 .addComponent(registerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
129                 .addContainerGap(25, Short.MAX_VALUE))
130         );
131 
132         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
133         getContentPane().setLayout(layout);
134         layout.setHorizontalGroup(
135             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
136             .addGroup(layout.createSequentialGroup()
137                 .addContainerGap()
138                 .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
139                 .addContainerGap())
140         );
141         layout.setVerticalGroup(
142             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
143             .addGroup(layout.createSequentialGroup()
144                 .addContainerGap()
145                 .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
146                 .addContainerGap())
147         );
148 
149         pack();
150     }// </editor-fold>                        
151 
152     private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
153         // TODO add your handling code here:
154     }                                           
155 
156     private void loginButtonMouseReleased(java.awt.event.MouseEvent evt) {                                          
157         // TODO add your handling code here:
158         //login();
159     }                                         
160 
161     private void registerButtonMouseClicked(java.awt.event.MouseEvent evt) {                                            
162         // TODO add your handling code here:
163         Register registerForm = new Register();
164         registerForm.setVisible(true);
165         
166     }                                           
167 
168     private void loginButtonMouseClicked(java.awt.event.MouseEvent evt) {                                         
169         // TODO add your handling code here:
170         login();
171     }                                        
172 
173     /**
174      * @param args the command line arguments
175      */
176     public static void main(String args[]) {
177         /* Set the Nimbus look and feel */
178         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
179         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
180          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
181          */
182         try {
183             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
184                 if ("Nimbus".equals(info.getName())) {
185                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
186                     break;
187                 }
188             }
189         } catch (ClassNotFoundException ex) {
190             java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
191         } catch (InstantiationException ex) {
192             java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
193         } catch (IllegalAccessException ex) {
194             java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
195         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
196             java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
197         }
198         //</editor-fold>
199         //</editor-fold>
200         //</editor-fold>
201         //</editor-fold>
202 
203         /* Create and display the form */
204         java.awt.EventQueue.invokeLater(new Runnable() {
205             public void run() {
206                 new Login().setVisible(true);
207             }
208         });
209     }
210     
211     public void login(){
212                
213         String usernameLogin = usernameTF.getText();
214         String passwordLogin = passwordTF.getText();
215         
216         boolean isLogin = false;
217         
218         String path = "User.txt";
219         File file = new File(path);
220         
221         String line = null,email = null,user = null,pass=null,id=null;
222         
223         try{
224             BufferedReader br = new BufferedReader(new FileReader(file));
225             while((line = br.readLine()) != null){
226                 
227                 String [] array = line.split("\\|");
228                 
229                 id = array[0];
230                 user = array[1];
231                 email = array[2];
232                 pass = array[3];
233                 
234                 if(usernameLogin.equals(email) && passwordLogin.equals(pass)){
235                     isLogin = true;
236                     if(isLogin){
237                         
238                         Home homeForm = new Home(id,user,email,pass);
239                         //Home homeForm = new Home();
240                         homeForm.setVisible(true);
241                         this.setVisible(false);
242                         
243                     } else {
244                         
245                         int m = JOptionPane.showConfirmDialog(
246                         null,
247                         "Login Unsuccessful",
248                         "Try again?",
249                         JOptionPane.YES_NO_OPTION);
250                         
251                     }
252                 } 
253                 
254             }
255         } catch (IOException e){
256             e.printStackTrace();
257         }
258         
259         
260             
261     }
262 
263     // Variables declaration - do not modify                     
264     private javax.swing.JLabel jLabel1;
265     private javax.swing.JPanel jPanel2;
266     private javax.swing.JToggleButton loginButton;
267     private javax.swing.JLabel passwordLabel;
268     private javax.swing.JTextField passwordTF;
269     private javax.swing.JButton registerButton;
270     private javax.swing.JLabel registerLabel;
271     private javax.swing.JLabel usernameLabel;
272     private javax.swing.JTextField usernameTF;
273     // End of variables declaration                   
274 
275 }
276