Staff Attendence

Staff Attendence

Above UI we are going to create by Awt for Showing staff attendance window in our Attendance 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.

    
        staffAttendanceFrame = new JFrame();
	    Utils.centeredFrame(staffAttendanceFrame, windowWidth, windowHeight, "Staff Attendence");
        Container frameContainer = staffAttendanceFrame.getContentPane();
		frameContainer.setBackground(Color.gray);

		// Changeable According to What size of screen You Want?
		int containerWidth = windowWidth*94/100;
		int containerHeight = windowHeight*90/100;
		
		int panelWidth = containerWidth*98/100;
		int panelHeight = containerHeight*96/100;
		int posX = windowWidth / 2 - panelWidth / 2  -frameControllerSize/5 ;
		int posY = windowHeight / 2 - panelHeight / 2 - frameControllerSize / 2;

		// frame without control options
		JPanel panel = new JPanel(); 
		panel.setLayout(null);//new BorderLayout());
		panel.setBackground(Color.decode("#FFFFFF")); 
		panel.setSize(new Dimension(panelWidth,panelHeight)); 
		panel.setLocation(posX, posY);
		frameContainer.add(panel);
		
		// for Rounded Background
		posX = windowWidth / 2 - containerWidth / 2 -frameControllerSize/5 ;
		posY = windowHeight / 2 - containerHeight / 2 - frameControllerSize / 2;
		JComponent jcomponent = new Background(posX, posY, containerWidth, containerHeight);
		jcomponent.setLayout(null);
		frameContainer.add(jcomponent, BorderLayout.CENTER);
    

First we create heading label.



        // for heading Layout
		posY = panelHeight * 2/100;
		Font headingFont = new Font("Serif", Font.PLAIN, 30);
		
		// header				
		JLabel heading_text = new JLabel("Staff Attendance");
		heading_text.setHorizontalAlignment(JLabel.CENTER);
		heading_text.setVerticalAlignment(JLabel.CENTER);
		heading_text.setBackground(Color.decode("#80c2b2"));
		heading_text.setForeground(Color.black);
		heading_text.setFont(headingFont);
		heading_text.setBounds(0,0, panelWidth, panelHeight / 9);
		panel.add(heading_text);
						
		panel.add(Utils.getSeparator(0, panelHeight / 7, panelWidth, 5,"000000"));	
    

Now we create a Attendance panel & it's UI.


        JPanel leftside =  Utils.createPanel(panelWidth,panelHeight*83/100,new Point(0,panelHeight/6), true,"Attendance");
        panel.add(leftside,BorderLayout.CENTER);
        leftside.setLayout(null);
 
        Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
        TitledBorder searchTittle = BorderFactory.createTitledBorder(loweredetched, "Search");
        searchTittle.setTitleJustification(TitledBorder.LEFT);
               
        //create label for search
        search=new JLabel();
        search.setText("Search staff");
        search.setForeground(Color.black);
        search.setFont(new Font("Lucida",Font.PLAIN,14));
        search.setSize(new Dimension(panelWidth/3,panelHeight/10));
        search.setLocation(30, 0); 
        leftside.add(search);

        //create TextField for search
        searchTextField = new JTextField();
        searchTextField.setSize(new Dimension(panelWidth/3,panelHeight/12));
        searchTextField.setLocation(30,panelHeight/15);	
        searchTextField.setBorder(searchTittle);
        leftside.add(searchTextField);
        

Here the code for how the search works.


        searchTextField.getDocument().addDocumentListener(new DocumentListener() {
	         @Override
	         public void insertUpdate(DocumentEvent e) {

	            search(searchTextField.getText());
	            
	         }
	         @Override
	         public void removeUpdate(DocumentEvent e) {

	            search(searchTextField.getText());
	         }
	         @Override
	         public void changedUpdate(DocumentEvent e) {

	            search(searchTextField.getText());
	         }
	         public void search(String str) {
	            if (str.length() == 0) {
	            	
	  			  displaylistToTable(table,getTableListData());

	            } else {
	            	defaultdataModel.setRowCount(0);
	            	
	            	displaylistToTable(table,getselectedTableListData(str));
	            }
	           }
	        });


    public ArrayList getselectedTableListData(String data){
		 ArrayList satffList=new ArrayList();
			 
			 ResultSet rs;
			 String query = "SELECT * FROM staff_data WHERE staff_id LIKE '%" + data + "%' or staff_name LIKE '%" + data + "%' " ;
		     Connection conn = SQLiteJDBCDriverConnection.connect1();
		     Statement stat = null;
				try {
					stat = conn.createStatement();
					 rs = stat.executeQuery(query);
					 AttendenceData staffdata;
					 while(rs.next()){
						 staffdata=new AttendenceData(rs.getString("staff_id"), rs.getString("staff_name"), rs.getString("staff_contact_no"), rs.getString("staff_email_id"), rs.getString("is_present"),rs.getString("present_date"));
						 satffList.add(staffdata);
						 
					 }
					 
					   rs.close();
					   stat.close();
				       conn.close();  
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		  
			      
			  return satffList;
			 
		 } 

    

Here we create our table which showing the staff attendance data.


        table = new JTable();			 
		displaylistToTable(table,getTableListData());			
	    table.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

		JTableHeader tableHeader21 = table.getTableHeader();
		tableHeader21.setDefaultRenderer(new KeepSortIconHeaderRenderer(tableHeader21.getDefaultRenderer()));
		  
		JScrollPane pane = new JScrollPane(table);
	    pane.setLocation(30, panelHeight/7+panelHeight/70);
	    pane.setSize(new Dimension(panelWidth-60,panelHeight*6/10));
	    leftside.add(pane);			  
	    table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
	    public void valueChanged(ListSelectionEvent event) {

	      	ArrayList selectedStaffDetailsList=getSelectedStaffDetails(table.getValueAt(table.getSelectedRow(),0).toString());
	        System.out.println(selectedStaffDetailsList);

	      	if(selectedStaffDetailsList.isEmpty()){
	      		JOptionPane.showMessageDialog(staffAttendanceFrame, "No Data Found");
	      	}else{
	      		new StaffAttendanceDetails(staffAttendanceFrame, totalwidth, totalheight, selectedStaffDetailsList);
	      		
	    	}
	      }
	    });

        staffAttendanceFrame.setVisible(true);
        panel.revalidate();
        panel.repaint();

    

Here we retrieve data from database and display in the table.


    public ArrayList getTableListData(){
		ArrayList staffList=new ArrayList();
			 
			ResultSet rs;
			String query = "SELECT * FROM staff_data;" ;
		    Connection conn = SQLiteJDBCDriverConnection.connect1();
            Statement stat = null;
				try {
					stat = conn.createStatement();
					rs = stat.executeQuery(query);
					AttendenceData staffdata;
					while(rs.next()){
						staffdata=new AttendenceData(rs.getString("staff_id"), rs.getString("staff_name"), rs.getString("staff_contact_no"), rs.getString("staff_email_id"), rs.getString("is_present"),rs.getString("present_date"));
						staffList.add(staffdata);
					}
					 
					stat.close();
					conn.close();   

				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			  return staffList;
		 }

         public void displaylistToTable(JTable table,ArrayList productList){
		 
		 defaultdataModel=new DefaultTableModel();
		 
		 Object []row=new Object[4];

		 defaultdataModel.setColumnIdentifiers(new Object[]{"Staff Id","Staff Name","Contact No","Email Id",});
		 
		 for (int i = 0; i < productList.size(); i++) {
			 
			 row[0]=productList.get(i).getId();
			 row[1]=productList.get(i).getStaffName();
			 row[2]=productList.get(i).getStaffContactNo();
			 row[3]=productList.get(i).getStaffEmail();

			 defaultdataModel.addRow(row);
		}
		 defaultdataModel.isCellEditable(0, 0);
		 table.setModel(defaultdataModel);
		 DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
	      centerRenderer.setHorizontalAlignment( JLabel.CENTER );
		  table.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );
		  table.getColumnModel().getColumn(1).setCellRenderer( centerRenderer );
		  table.getColumnModel().getColumn(2).setCellRenderer( centerRenderer );
		  table.getColumnModel().getColumn(3).setCellRenderer( centerRenderer );

		  table.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
	      table.setRowHeight(30);
	 }
    

when we select any row from the table get the seleceted row data.


    public ArrayList getSelectedStaffDetails(String staffId){
		System.out.println("getSelectedStaffDetails");
		ArrayList selectedStaffDetailsList=new ArrayList();

			  ResultSet rs;
			  String sql = "SELECT * FROM attendance WHERE staff_id = '"+staffId+"'";
		       Connection conn = SQLiteJDBCDriverConnection.connect1();
		     
		      
		      Statement stat = null;
				try {
					stat = conn.createStatement();
					 rs = stat.executeQuery(sql);
					 SelectedStaffData staffdata;
					 while(rs.next()){
						 staffdata=new SelectedStaffData(rs.getString("staff_id"), rs.getString("staff_name"), rs.getString("status"), rs.getString("present_dates"));
						 selectedStaffDetailsList.add(staffdata);
					 }
					 
					 stat.close();
					 conn.close();

				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
	      

			  return selectedStaffDetailsList;
			 
		 }

         

At last we will manage closing of window

    
        staffAttendanceFrame.addWindowListener(new java.awt.event.WindowAdapter() {
	            @Override
	            public void windowClosing(java.awt.event.WindowEvent windowEvent) {
	        		staffAttendanceFrame.dispose();
	        		attendanceFrame.setVisible(true);
	            }
	        });	 
                       

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.