Dashboard

Dashboard

Above UI we are going to create by Awt for Showing dashboard 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.

    
        attendanceFrame = new JFrame();
		Utils.centeredFrame(attendanceFrame, windowWidth, windowHeight, "Attendance System");
		attendanceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//loginFrame.setResizable(false);
		
		Container frameContainer = attendanceFrame.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.setOpaque(false);
		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);
    

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

    
public class Background extends JComponent {
	
	int width, height,setX,setY;

    public Background (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.setPaint(Color.white);
        
        RenderingHints qualityHints = new RenderingHints(
        		  RenderingHints.KEY_ANTIALIASING,
        		  RenderingHints.VALUE_ANTIALIAS_ON );
        		qualityHints.put(
        		  RenderingHints.KEY_RENDERING,
        		  RenderingHints.VALUE_RENDER_QUALITY );
        		g2.setRenderingHints( qualityHints );

        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);

    }   
}
    

Here we create Label for our heading and create "LOGOUT" button.

    
        // for heading Layout
		posY = panelHeight * 2/100;
		Font headingFont = new Font("Serif", Font.PLAIN, 36);

		// header
		JLabel heading_text = new JLabel("Attendance System");
		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, panelHeight / 100, panelWidth, panelHeight / 9);
		panel.add(heading_text);
		panel.add(Utils.getSeparator(0, panelHeight / 7, panelWidth, 5,"000000"));	

        headingFont = new Font("Serif", Font.PLAIN, 15);
		JLabel admin_text = new JLabel("Hi Admin");
		admin_text.setHorizontalAlignment(JLabel.LEFT);
		admin_text.setVerticalAlignment(JLabel.CENTER);
		admin_text.setBackground(Color.decode("#80c2b2"));
		admin_text.setForeground(Color.black);
		admin_text.setFont(headingFont);
		admin_text.setBounds(panelWidth*2/100,panelHeight/50, panelWidth/2, panelHeight / 10);
		panel.add(admin_text);
		
		panel.add(getLogoutButton(panelWidth, panelHeight));

    

Here we define our "LOGOUT" button.

    
    private JButton getLogoutButton(int width, int height) {
		  
          logoutBtn = new JButton(new ImageIcon(((new ImageIcon("images/button37.png")).getImage()).getScaledInstance( width*12/100, height*5/100, java.awt.Image.SCALE_SMOOTH)));
          logoutBtn.setContentAreaFilled(false);
          logoutBtn.setBorder(null);
          logoutBtn.setText("LOGOUT");
          logoutBtn.setHorizontalTextPosition(JButton.CENTER);
          logoutBtn.setVerticalTextPosition(JButton.CENTER);
          logoutBtn.setBounds(width*83/100,(int) (height/23), width*12/100, height*5/100); 
          logoutBtn.setFont(new java.awt.Font("Serif",0, 10));
          logoutBtn.setForeground(Color.white);
          
          logoutBtn.addActionListener(new ActionListener() {
              
              @Override
              public void actionPerformed(ActionEvent e) {
                  // TODO Auto-generated method stub
                  moveToLogin();

              }
          });
          
          return logoutBtn;		
      }
  
  
    private void moveToLogin() {
           attendanceFrame.dispose();
           LoginWindow=new Login(totalwidth, totalheight);
           LoginWindow.getFrameRef().setVisible(true);
           updateisLoginValue("0");
  
          }

    

Here we create Attendence panel and 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);
       
        allStaffLabel=new JLabel();
        allStaffLabel.setText("All Staff");
        allStaffLabel.setForeground(Color.black);
        allStaffLabel.setFont(new Font("Lucida",Font.BOLD,30));
        allStaffLabel.setSize(new Dimension(panelWidth/3,panelHeight/10));
        allStaffLabel.setLocation(100, 0); 
        leftside.add(allStaffLabel);
    
        dateLabel=new JLabel();
        dateLabel.setText("Date : "+strDate);
        dateLabel.setForeground(Color.black);
        dateLabel.setFont(new Font("Lucida",Font.BOLD,30));
        dateLabel.setSize(new Dimension(panelWidth/3,panelHeight/10));
        dateLabel.setLocation(panelWidth*2/3, 0); 
        leftside.add(dateLabel);
        
		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/10);
	    pane.setSize(new Dimension(panelWidth-60,panelHeight*6/10));
	    leftside.add(pane);	
	      
	    table.addMouseListener(new java.awt.event.MouseAdapter() {
	    	    @Override
	    	    public void mouseClicked(java.awt.event.MouseEvent evt) {
	    	        int row = table.rowAtPoint(evt.getPoint());
	    	        int col = table.columnAtPoint(evt.getPoint());
	    	        
	    	        if(row == -1)
					{
					   JOptionPane.showMessageDialog(attendanceFrame, "Please selecet Staff");
					}
					else
					{
			        	  if (row >= 0 && col >= 0) {
			        		  
			        		  if(col==0){
	    					    String Table_click = (table.getModel().getValueAt(row, 1).toString());\

	    					    if (table.getModel().getValueAt(row, 0).toString().equals("true")){
	    							tableIntryField(table.getModel().getValueAt(row, 1).toString(), table.getModel().getValueAt(row,2).toString(), table.getModel().getValueAt(row,3).toString());
	    						    updateTable(Table_click,"present");

	    					    }else{
	    						    updateTable(Table_click,"absent");
	    					    					    	
	    					    }
			        		  }
			        }	    					    
					    // do whatever you need to do with the data from the row
					}
	    	    }
	    	});  
    

Here we create two buttons "Manage Staff" & "Show Attendence".

    
        JButton manageStaff = new JButton(new ImageIcon(((new ImageIcon("images/button37.png")).getImage()).getScaledInstance(300, 60, java.awt.Image.SCALE_SMOOTH)));
	    manageStaff.setContentAreaFilled(false);
        manageStaff.setBorder(null);
	    manageStaff.setText("Manage Staff");
	    manageStaff.setForeground(Color.white);
        manageStaff.setFont(new Font("Lucida",Font.PLAIN,16));
	    manageStaff.setHorizontalTextPosition(JButton.CENTER);
	    manageStaff.setVerticalTextPosition(JButton.CENTER);
        manageStaff.setBounds(panelWidth/15, panelHeight*72/100,300,60);  
	    manageStaff.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	    leftside.add(manageStaff);
	        
	        
	    manageStaff.addActionListener(new ActionListener() {
				
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				attendanceFrame.setVisible(false);
				new Addstaff(totalwidth, totalheight, attendanceFrame);
				
    			}
			});

	    JButton showattendence = new JButton(new ImageIcon(((new ImageIcon("images/button37.png")).getImage()).getScaledInstance(300, 60, java.awt.Image.SCALE_SMOOTH)));
	    showattendence.setContentAreaFilled(false);
        showattendence.setBorder(null);
	    showattendence.setText("Show Attendence");
	    showattendence.setForeground(Color.white);
        showattendence.setFont(new Font("Lucida",Font.PLAIN,16));
	    showattendence.setHorizontalTextPosition(JButton.CENTER);
	    showattendence.setVerticalTextPosition(JButton.CENTER);
        showattendence.setBounds(panelWidth-300-panelWidth/15, panelHeight*72/100,300,60);  
	    showattendence.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
	    leftside.add(showattendence);

	    showattendence.addActionListener(new ActionListener() {				
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				new StaffAttendance(attendanceFrame, totalwidth, totalheight);					
			}
		});
   
        attendanceFrame.setVisible(true);
	    panel.revalidate();
	    panel.repaint();	
	
    

We have created a Database named "attendence_db.db", lets connect that database and put that code in seperate class "SQLiteJDBCDriverConnection" because we are going to use it multiple time in our project.

    
public class SQLiteJDBCDriverConnection {
    /**
    * Connect to a sample database
    */
   public String rate=null;
   public String date=null;


public static Connection connect1() {
   try {
       // SQLite connection string
       Class.forName("org.sqlite.JDBC");
    } catch (ClassNotFoundException ex) {
           Logger.getLogger(SQLiteJDBCDriverConnection.class.getName()).log(Level.SEVERE, null, ex);
    }
    String url = "jdbc:sqlite:attendance_db.db";
    Connection conn = null;
    try {
           conn = DriverManager.getConnection(url);
    } catch (SQLException e) {
           System.out.println(e.getMessage());
    }
    return conn;
    }
}


    

After Table creation we need to fetch the data from database and display in 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);
					 }
					 
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
	      
			  return staffList;
			 
		 }
    
    public void displaylistToTable(JTable table,ArrayList productList){
		 
		 defaultdataModel=new DefaultTableModel(){
			 public Class getColumnClass(int column){
				 
				 switch(column){
				 case 0:
					 return Boolean.class;
				 case 1:
					 return String.class;
				 case 2:
					 return String.class;
					 
				 case 3:
					 return String.class;
					 
				 case 4:
					 return String.class;
				 }

				 return null;
				 
			 }
		 };
		 
		 Object []row=new Object[5];

		 defaultdataModel.setColumnIdentifiers(new Object[]{"Absent/Present","Staff Id","Staff Name","Contact No","Email Id",});
		 
		 for (int i = 0; i < productList.size(); i++) {
			 
			 if(productList.get(i).getIsPresent().equalsIgnoreCase("present") && productList.get(i).getDate().equalsIgnoreCase(strDate) ){				 
				 row[0]=true;
			 }else{
				 row[0]=false;

			 }
			 row[1]=productList.get(i).getId();
			 row[2]=productList.get(i).getStaffName();
			 row[3]=productList.get(i).getStaffContactNo();
			 row[4]=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(1).setCellRenderer( centerRenderer );
		  table.getColumnModel().getColumn(2).setCellRenderer( centerRenderer );
		  table.getColumnModel().getColumn(3).setCellRenderer( centerRenderer );
		  table.getColumnModel().getColumn(4).setCellRenderer( centerRenderer );
		  table.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));    
          table.setRowHeight(30); 
	 }
    

Here we set table entry field.

    
	public boolean tableIntryField(String generatedID, String nameString,String contactString) {
		 
        Connection conn=SQLiteJDBCDriverConnection.connect1();
       boolean issuccess=false;
       Statement stmt = null;
       int action;

       
       String sql = "INSERT INTO attendance (staff_id, staff_name, status,present_dates) " +
               "VALUES (?,?,?,?);"; 

       PreparedStatement pst  = null;
       ResultSet rs = null;
       try {
               pst = conn.prepareStatement(sql);
               pst.setString(1, generatedID);
               pst.setString(2, nameString);
               pst.setString(3, "present");
               pst.setString(4, strDate);
             

               action = pst.executeUpdate();
               if(action>0){
                      issuccess= true;
    
                  }else{
                      issuccess= false;

                  }
              
           } catch (Exception a) {
               a.printStackTrace();
           } finally {
               if(rs != null){
                    try{
                         rs.close();
                    } catch(Exception e){
                        e.printStackTrace();
                    }
               }
               if(pst != null){
                   try{
                       pst.close();
                   } catch(Exception e){
                       e.printStackTrace();
                   }
               }
           }
            return issuccess;

}
    

Here we manage the Attendence status of staff members and update in database.


public void updateTable(String matchValue,String updateValue) {
			 
    Connection conn=SQLiteJDBCDriverConnection.connect1();
    boolean issuccess=false;
    Statement stmt = null;
    int action;	

    String sqlUpdate = "UPDATE staff_data "
           + "SET is_present = ?, "
           + "present_date = ? "
           + "WHERE staff_id = ?";

      PreparedStatement pst  = null;
      ResultSet rs = null;
      try {
          pst = conn.prepareStatement(sqlUpdate);

          pst.setString(1, updateValue);
          pst.setString(2, strDate);
          pst.setString(3, matchValue);

          action = pst.executeUpdate();
              

              if(action>0){
                     issuccess= true;
   
                 }else{
                     issuccess= false;

                 }
             
          } catch (Exception a) {
              a.printStackTrace();
          } finally {
              if(rs != null){
                   try{
                        rs.close();
                   } catch(Exception e){
                       e.printStackTrace();
                   }
              }
              if(pst != null){
                  try{
                      pst.close();
                  } catch(Exception e){
                      e.printStackTrace();
                  }
              }
          }
        }


    

Here we check the admin is login aur not.


public void updateisLoginValue(String updatValue) {
		 
    Connection conn=SQLiteJDBCDriverConnection.connect1();
    boolean issuccess=false;
    Statement stmt = null;
    int action;		            
    String newValue = updatValue;
    String  idToMatch = "IS_LOGIN";

    String sqlUpdate = "UPDATE system_setting "
            + "SET value = ? "
            + "WHERE key = ?";
      

       PreparedStatement pst  = null;
       ResultSet rs = null;
       try {
        pst = conn.prepareStatement(sqlUpdate);
           pst.setString(1, newValue);
           pst.setString(2, idToMatch);	       	
                action = pst.executeUpdate();
                if(action>0){
                       issuccess= true;
     
                   }else{
                       issuccess= false;

                   }
               
           } catch (Exception a) {
               a.printStackTrace();
           } finally {
               if(rs != null){
                    try{
                         rs.close();
                    } catch(Exception e){
                        e.printStackTrace();
                    }
               }
               if(pst != null){
                   try{
                       pst.close();
                   } catch(Exception e){
                       e.printStackTrace();
                   }
               }
           }
    }
    

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.