Login

Login

Above UI we are going to create by AWT for login staff in our Hostel management system desktop application. For that first we create a window and configure its title, size and color. Here we are creating a window at Top Level so we can add this window at the top of any other window.

    
    	frame = new JFrame("Login");
	    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	    frame.pack();
		Utils.centeredFrame(frame, winWidth,winHeight, "Login");

	    frame.setVisible(true);
	    frame.setResizable(false);
	        
	    Container mainContainer=frame.getContentPane();
	    mainContainer.setBackground(Color.gray);

	    checkLogin();	
	    

	        
	    JPanel panel = new JPanel();
	    panel.setLayout(null);
		panel.setOpaque(false);
		panel.setBackground(Color.BLACK);
		panel.setSize(new Dimension(winWidth/3,winHeight*7/10));
		panel.setLocation(winWidth/2-winWidth/6,winHeight/10);
		mainContainer.add(panel,BorderLayout.CENTER);
    

After creation of window, we will add a AWT background.

    
	JComponent jcomponent=new RoundedBackground(winWidth/2-winWidth/6,winHeight/10,winWidth/3,winHeight*7/10);// 
	mainContainer.add(jcomponent,BorderLayout.CENTER);

    public class RoundedBackground extends JComponent {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	int width, height,setX,setY;

    public RoundedBackground (int setX,int setY,int width, int height) {
    	this.width=width;
    	this.height=height;
    	this.setX=setX;
    	this.setY=setY;
    	
       setSize(width, height);
    }
	
    @Override
    public void paint(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setPaint(Color.white);


        double x = setX;
        double y = setY;
        double w = width;
        double h = height;
        RoundRectangle2D round=new RoundRectangle2D.Double(x, y, w, h, 80, 80);
        g2.fill(round);

    }

    
}
    

So we are done with all frame and background UI. Now we will create Loging UI.



            #ADD Title
            JLabel label=new JLabel();
	        label.setText("Login");
	        label.setBackground(Color.red);
	        label.setForeground(Color.black);
	        label.setSize(new Dimension(winWidth/3,winHeight/10));
	        label.setFont(new Font("Lucida",Font.PLAIN,48));
	        label.setAlignmentX(JLabel.CENTER_ALIGNMENT);
	        label.setLocation(150, 50); 
	        panel.add(label, BorderLayout.CENTER);
	        
	        
	      
	        #ADD USERNAME & its Style
	        JLabel user_name=new JLabel();
	        user_name.setText("User name");
	        user_name.setBackground(Color.red);
	        user_name.setForeground(Color.black);
	        user_name.setFont(new Font("Lucida",Font.PLAIN,12));
	        user_name.setSize(new Dimension(winWidth/3,winHeight/10));
	        user_name.setLocation(50, 165); 
	        panel.add(user_name, BorderLayout.SOUTH);
	         Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);

	      
	        
	        TitledBorder usernametittle = BorderFactory.createTitledBorder(loweredetched, "User Name");
	        usernametittle.setTitleJustification(TitledBorder.LEFT);
	        
            TitledBorder passwordTittled = BorderFactory.createTitledBorder(loweredetched, "Password");
            passwordTittled.setTitleJustification(TitledBorder.LEFT);

	        usernameField = new JTextField();
	        usernameField.setSize(new Dimension(winWidth/4,winHeight/15));
	        usernameField.setLocation(50, 230);	
	        usernameField.setBorder(usernametittle);
	        panel.add(usernameField,BorderLayout.CENTER);

	        panel.validate();
	        panel.repaint();
	            

	        #ADD PASSWORD & its Style
	        JLabel password=new JLabel();
	        password.setText("password");
	        password.setBackground(Color.red);
	        password.setForeground(Color.black);
	        password.setFont(new Font("Lucida",Font.PLAIN,12));	        
	        password.setSize(new Dimension(winWidth/3,winHeight/10));
	        password.setLocation(50, 265); 
	        panel.add(password, BorderLayout.SOUTH);
	        
	        
	        passwordField = new JPasswordField();
	        passwordField.setSize(new Dimension(winWidth/4,winHeight/15));
	        passwordField.setLocation(50, 330);	       
	        panel.add(passwordField,BorderLayout.CENTER);
	        passwordField.setBorder(passwordTittled);
	        
	        

	        panel.validate();
	        panel.repaint();

	        
            #ADD LOGIN BUTTON & its Style
	        JButton loginButton = new JButton(new ImageIcon(((new ImageIcon("images/button32.png")).getImage()).getScaledInstance(200, 50, java.awt.Image.SCALE_SMOOTH)));
	        loginButton.setContentAreaFilled(false);
	        loginButton.setBorder(null);
	        loginButton.setText("LOGIN");
	        loginButton.setHorizontalTextPosition(JButton.CENTER);
	        loginButton.setVerticalTextPosition(JButton.CENTER);
	        loginButton.setOpaque(false);
	        loginButton.setContentAreaFilled(false);
	        loginButton.setBorderPainted(false);
	        loginButton.setFont(new java.awt.Font("Serif",1, 15));
	        loginButton.setForeground(Color.white);
	        loginButton.setBounds(130,450,200,50);  
	        panel.add(loginButton);
        

    

Here is logic to login Admin :

    
			loginButton.addActionListener(new ActionListener() {
	            @Override
	            public void actionPerformed(ActionEvent ae) {
	            	
	            String usernameString = usernameField.getText();
	            String passString = passwordField.getText();
	            String dbUserName = "";
	            String dbPassword = "";
	            String key1 = "ADMIN_ID";
	            String key2 = "ADMIN_PASS";
	            	 
	            if(!usernameString.isEmpty() && !passString.isEmpty()) {
	            	conn = SQLiteJDBCDriverConnection.connect1();
	            	try {
						insert = conn.prepareStatement("SELECT value FROM system_setting WHERE key = 'ADMIN_ID'");
						
						ResultSet rs = insert.executeQuery();
						 
						while (rs.next()) {    
							dbUserName = rs.getString(1);  
						}
	            	
						insert = conn.prepareStatement("SELECT value FROM system_setting WHERE key = 'ADMIN_PASS'");
						ResultSet rs2 = insert.executeQuery();
							 
						while (rs2.next()) {    
							dbPassword = rs2.getString(1);  
						}	
						
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					 

						  
					if( usernameString.equals(dbUserName) && passString.equals(dbPassword)) {
						
						String key_is_logined = "IS_LOGINED";
					            
						try {
							insert = conn.prepareStatement("UPDATE system_setting "+ "SET value = ? "+ "WHERE key = ?");
														insert.setString(1, "1");insert.setString(2, key_is_logined);
							insert.executeUpdate();
							conn.close();
						} catch (SQLException e3) {
							// TODO Auto-generated catch block
							e3.printStackTrace();
						}
					            
					navigate_to_dashboard();
						
					}else {
							JOptionPane.showMessageDialog(frame, "Unmatched");
					}
					
	            }else {
	            	String var = "Usdername Should not be empty,  Password Should not be empty";
	            	JOptionPane.showMessageDialog(frame, var);
	            }
						 
	  	
	            }
	        });	        
	        
	       
	        
	        panel.revalidate();
	        panel.repaint();
    

Here's logic for check Loged or Not , Also show Loged Staff Name.

    
    private void checkLogin() {
		 
		 
		 String key_id = "IS_LOGINED";
		 String loginKey = "";
		 String adminId = "";
		 
		 conn = SQLiteJDBCDriverConnection.connect1();
    	 try {
			insert = conn.prepareStatement("SELECT value FROM system_setting WHERE key = 'IS_LOGINED'");
			
			ResultSet rs = insert.executeQuery();
			 
			 while (rs.next()) {    
				 loginKey = rs.getString(1);  
		        }
			 
			 if(loginKey.equals("1")) {
				 
				 insert = conn.prepareStatement("SELECT value FROM system_setting WHERE key = 'ADMIN_ID'");
					
					ResultSet rs2 = insert.executeQuery();
					 
					 while (rs2.next()) {    
						 adminId = rs2.getString(1);  
				        }
					 conn.close();
				 
					 AppConstant.STAFF_NAME = adminId;
					 navigate_to_dashboard();
			 }
			 
		 
    	 } catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
		 
	 }
        
        

Navigate to to DashBoard .

    

    private void navigate_to_dashboard() {
		 new DashboardNew(frame);
	 }}

        

Programmer Mirta is for learning and training. Projects might be simple to improve learning. Projects are constantly reviewed to avoid errors, but we cannot assure full correctness of all content. While using Programmer Mitra, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 2021 by Programmer Mitra. All Rights Reserved.