Advertisement
Guest User

Untitled

a guest
Jan 26th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.45 KB | None | 0 0
  1. UI
  2. package ui;
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.awt.Toolkit;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import java.awt.event.MouseEvent;
  9. import java.awt.event.MouseListener;
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.SQLException;
  13.  
  14. import javax.swing.ImageIcon;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JOptionPane;
  18. import javax.swing.JPanel;
  19. import javax.swing.JPasswordField;
  20. import javax.swing.JTextField;
  21.  
  22. import techServ.UserDA;
  23. import domain.User;
  24.  
  25. public class LoginUI extends JFrame
  26. {
  27. private JPanel panel;
  28. private JTextField usernameTF;
  29. private JLabel loginB, usernameL, passwordL;
  30. private JPasswordField password;
  31. private String p;
  32.  
  33. private User user;
  34.  
  35. private UserDA userDA;
  36.  
  37. private Connection connection;
  38.  
  39. private LoginHandler loginHandler;
  40.  
  41. LoginUI()
  42. {
  43. // Remove border and Title bar of the Window
  44. setUndecorated(true);
  45.  
  46. setSize(350,250);
  47. setLayout(null);
  48.  
  49. // Method for initialization of the objects declared.
  50. initializedComponents();
  51.  
  52. // Method for Setting Objects their specific bounds in the window.
  53. setComponentsBounds();
  54.  
  55. // Method for Setting Panels.
  56. setPanel();
  57.  
  58. loginHandler = new LoginHandler();
  59. loginB.addMouseListener(loginHandler);
  60. usernameTF.addKeyListener(loginHandler);
  61. password.addKeyListener(loginHandler);
  62.  
  63. // Adding panel to the Frame.
  64. add(panel);
  65.  
  66. // Setting the Frame to the center of screen.
  67. setLocationRelativeTo(null);
  68. setDefaultCloseOperation(EXIT_ON_CLOSE);
  69. setVisible(true);
  70. setResizable(false);
  71. repaint();
  72. }
  73.  
  74. private class LoginHandler implements MouseListener, KeyListener
  75. {
  76.  
  77. public void mouseClicked(MouseEvent e){
  78. if(loginB == e.getSource()){
  79. validateUser();
  80. }
  81. }
  82. public void mouseEntered(MouseEvent e){}
  83. public void mouseExited(MouseEvent e){}
  84. public void mousePressed(MouseEvent e)
  85. {
  86. loginB.setIcon(new ImageIcon(getClass().getResource("LoginButton2.png")));
  87. repaint();
  88. }
  89. public void mouseReleased(MouseEvent e){}
  90. public void keyPressed(KeyEvent e)
  91. {
  92. if(e.getKeyCode() == KeyEvent.VK_ENTER){
  93. loginB.setIcon(new ImageIcon(getClass().getResource("LoginButton2.png")));
  94. validateUser();
  95. }
  96. }
  97. public void keyReleased(KeyEvent e) {}
  98. @Override
  99. public void keyTyped(KeyEvent arg0) {}
  100. }
  101.  
  102. private void validateUser(){
  103. // Getting data in JPasswordField
  104. char[] passwordChar = password.getPassword();
  105. p = new String(passwordChar);
  106.  
  107. User user = new User();
  108. user.setUsername(usernameTF.getText());
  109. user.setPassword(p);
  110. // Access the Database
  111. userDA = new UserDA(getConnection(), user.getUsername());
  112.  
  113. if(user.getUsername().equals(userDA.getUser(user).getUsername()) &&
  114. user.getPassword().equals(userDA.getUser(user).getPassword()))
  115. {
  116. displayMain(userDA.getUser(user));
  117. dispose();
  118. }else
  119. {
  120. JOptionPane.showMessageDialog(null, "Invalid Username or Password\nPlease try again.",
  121. "Error Messsage", JOptionPane.INFORMATION_MESSAGE);
  122. loginB.setIcon(new ImageIcon(getClass().getResource("LoginButton1.png")));
  123. }
  124. }
  125.  
  126. public void displayMain(User user)
  127. {
  128. new MainUI(user, userDA);
  129.  
  130. }
  131.  
  132. public Connection getConnection(){
  133. try
  134. {
  135. Class.forName("com.ibm.db2.jcc.DB2Driver");
  136. connection = DriverManager.getConnection("jdbc:db2://localhost:50000/hrms2","Karl Louie", "fingerprintaz");
  137. }
  138. catch (ClassNotFoundException e)
  139. {
  140. e.printStackTrace();
  141. }
  142. catch (SQLException e)
  143. {
  144. e.printStackTrace();
  145. }
  146.  
  147. return connection;
  148. }
  149.  
  150. private void setPanel()
  151. {
  152. panel = new JPanel()
  153. {
  154. public void paintComponent(Graphics g)
  155. {
  156. Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("LoginBG.png"));
  157. g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
  158. }
  159. };
  160. panel.setLayout(null);
  161. panel.setBounds(0, 0, 350, 250);
  162. panel.add(usernameTF);
  163. panel.add(password);
  164. panel.add(loginB);
  165. }
  166.  
  167. private void initializedComponents()
  168. {
  169. usernameTF = new JTextField(15);
  170. password = new JPasswordField(15);
  171. loginB = new JLabel(new ImageIcon(getClass().getResource("LoginButton1.png")));
  172.  
  173. }
  174.  
  175. private void setComponentsBounds()
  176. {
  177. usernameTF.setBounds(155, 162, 150, 17);
  178. password.setBounds(155, 182, 150, 17);
  179. loginB.setBounds(133, 215, 93, 20);
  180.  
  181. }
  182.  
  183.  
  184.  
  185. public static void main (String[] args)
  186. {
  187. new LoginUI();
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement