Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- UI
- package ui;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Toolkit;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import javax.swing.ImageIcon;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JPasswordField;
- import javax.swing.JTextField;
- import techServ.UserDA;
- import domain.User;
- public class LoginUI extends JFrame
- {
- private JPanel panel;
- private JTextField usernameTF;
- private JLabel loginB, usernameL, passwordL;
- private JPasswordField password;
- private String p;
- private User user;
- private UserDA userDA;
- private Connection connection;
- private LoginHandler loginHandler;
- LoginUI()
- {
- // Remove border and Title bar of the Window
- setUndecorated(true);
- setSize(350,250);
- setLayout(null);
- // Method for initialization of the objects declared.
- initializedComponents();
- // Method for Setting Objects their specific bounds in the window.
- setComponentsBounds();
- // Method for Setting Panels.
- setPanel();
- loginHandler = new LoginHandler();
- loginB.addMouseListener(loginHandler);
- usernameTF.addKeyListener(loginHandler);
- password.addKeyListener(loginHandler);
- // Adding panel to the Frame.
- add(panel);
- // Setting the Frame to the center of screen.
- setLocationRelativeTo(null);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setVisible(true);
- setResizable(false);
- repaint();
- }
- private class LoginHandler implements MouseListener, KeyListener
- {
- public void mouseClicked(MouseEvent e){
- if(loginB == e.getSource()){
- validateUser();
- }
- }
- public void mouseEntered(MouseEvent e){}
- public void mouseExited(MouseEvent e){}
- public void mousePressed(MouseEvent e)
- {
- loginB.setIcon(new ImageIcon(getClass().getResource("LoginButton2.png")));
- repaint();
- }
- public void mouseReleased(MouseEvent e){}
- public void keyPressed(KeyEvent e)
- {
- if(e.getKeyCode() == KeyEvent.VK_ENTER){
- loginB.setIcon(new ImageIcon(getClass().getResource("LoginButton2.png")));
- validateUser();
- }
- }
- public void keyReleased(KeyEvent e) {}
- @Override
- public void keyTyped(KeyEvent arg0) {}
- }
- private void validateUser(){
- // Getting data in JPasswordField
- char[] passwordChar = password.getPassword();
- p = new String(passwordChar);
- User user = new User();
- user.setUsername(usernameTF.getText());
- user.setPassword(p);
- // Access the Database
- userDA = new UserDA(getConnection(), user.getUsername());
- if(user.getUsername().equals(userDA.getUser(user).getUsername()) &&
- user.getPassword().equals(userDA.getUser(user).getPassword()))
- {
- displayMain(userDA.getUser(user));
- dispose();
- }else
- {
- JOptionPane.showMessageDialog(null, "Invalid Username or Password\nPlease try again.",
- "Error Messsage", JOptionPane.INFORMATION_MESSAGE);
- loginB.setIcon(new ImageIcon(getClass().getResource("LoginButton1.png")));
- }
- }
- public void displayMain(User user)
- {
- new MainUI(user, userDA);
- }
- public Connection getConnection(){
- try
- {
- Class.forName("com.ibm.db2.jcc.DB2Driver");
- connection = DriverManager.getConnection("jdbc:db2://localhost:50000/hrms2","Karl Louie", "fingerprintaz");
- }
- catch (ClassNotFoundException e)
- {
- e.printStackTrace();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return connection;
- }
- private void setPanel()
- {
- panel = new JPanel()
- {
- public void paintComponent(Graphics g)
- {
- Image img = Toolkit.getDefaultToolkit().getImage(getClass().getResource("LoginBG.png"));
- g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
- }
- };
- panel.setLayout(null);
- panel.setBounds(0, 0, 350, 250);
- panel.add(usernameTF);
- panel.add(password);
- panel.add(loginB);
- }
- private void initializedComponents()
- {
- usernameTF = new JTextField(15);
- password = new JPasswordField(15);
- loginB = new JLabel(new ImageIcon(getClass().getResource("LoginButton1.png")));
- }
- private void setComponentsBounds()
- {
- usernameTF.setBounds(155, 162, 150, 17);
- password.setBounds(155, 182, 150, 17);
- loginB.setBounds(133, 215, 93, 20);
- }
- public static void main (String[] args)
- {
- new LoginUI();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement