Dashboard

Dashboard

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

    
        dashboardFrame = new JFrame();
		Utils.centeredFrame(dashboardFrame, windowWidth, windowHeight, "Expense Management System");
		dashboardFrame.setResizable(false);
		
		Container frameContainer = dashboardFrame.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 ;
		int posY = windowHeight / 2 - panelHeight / 2 - frameControllerSize / 2;

		//frame without control options
		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 ;
		posY = windowHeight / 2 - containerHeight / 2 - frameControllerSize / 2;
		JComponent jcomponent = new RoundedBackground(posX, posY, containerWidth, containerHeight);
		jcomponent.setLayout(null);
		frameContainer.add(jcomponent, BorderLayout.CENTER);

    

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

    
    posY = windowHeight / 2 - containerHeight / 2 - frameControllerSize / 2;
	JComponent jcomponent = new RoundedBackground(posX, posY, containerWidth, containerHeight);
	jcomponent.setLayout(null);
	frameContainer.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);

        }  
    }
    

Here we create Label for our heading.

    
    
		// for heading Layout
		posY = panelHeight * 2/100;
		Font headingFont = new Font("Serif", Font.PLAIN, 25);
		// header
				
		JLabel heading_text = new JLabel("Expense Management 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,0, panelWidth, panelHeight / 9);
		panel.add(heading_text);
		
        //Dashboard heading
		headingFont = new Font("Serif", Font.PLAIN, 15);
		JLabel heading_text_minor = new JLabel("Dashboard");
		heading_text_minor.setHorizontalAlignment(JLabel.CENTER);
		heading_text_minor.setVerticalAlignment(JLabel.CENTER);
		heading_text_minor.setBackground(Color.decode("#80c2b2"));
		heading_text_minor.setForeground(Color.black);
		heading_text_minor.setFont(headingFont);
		heading_text_minor.setBounds(0,panelHeight*6/100, panelWidth, panelHeight / 9);
		panel.add(heading_text_minor);
				
		panel.add(Utils.getSeparator(0, panelHeight / 7, panelWidth, 5,"000000"));	 
		panel.add(Utils.getSeparator(0, panelHeight*22/100, panelWidth, 5,"000000"));	

        
    

After we create another label and "LOGOUT" button.

    

        headingFont = new Font("Serif", Font.PLAIN, 12);
		JLabel admin_text = new JLabel("Hi  "+AppConstant.USER_NAME);
		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*13/100, panelWidth/2, panelHeight / 9);
		panel.add(admin_text);
		
		panel.add(getLogoutButton(panelWidth, panelHeight));

        
    
    

Here we define "LOGOUT" button and click functionality.

    
    private JButton getLogoutButton(int width, int height) {
		  
        logoutBtn = new JButton(new ImageIcon(((new ImageIcon("images/button38.png")).getImage()).getScaledInstance( width*16/100, height*6/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*14/100)+8, width*16/100, height*6/100); 
        logoutBtn.setFont(new java.awt.Font("Serif",1, 12));
        logoutBtn.setForeground(Color.white);
          
        logoutBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
                moveToLogin();
            }
        });
            return logoutBtn;		
        }
   

After we create a panel and its UI.

    
    JPanel actionPanel = new JPanel();
	    actionPanel.setLayout(null);
	    actionPanel.setBounds(panelWidth*15/100,panelHeight*70/100, panelWidth*70/100, panelHeight*28/100);
	    actionPanel.setBorder(Utils.getTitledBorder(16, "Actions"));
	    panel.add(actionPanel);
	    
	    actionPanel.add(addMoneyBtn(panelWidth, panelHeight));
	    actionPanel.add(addExpenseBtn(panelWidth, panelHeight));
	    actionPanel.add(statementBtn(panelWidth, panelHeight));
	    
	    PieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator(  
	            " {0} : {2}", new DecimalFormat("0"), new DecimalFormat("0%")); 
	    
	    getFirstPiechart(panelWidth, panelHeight, panel, labelGenerator);

	    getSecondPiechart(panelWidth, panelHeight, panel, labelGenerator);

	    getThirdPiechart(panelWidth, panelHeight, panel, labelGenerator);
        
	    // set visible window
	 	dashboardFrame.setVisible(true);

    

Here we define "ADDMONEY","EXPENSE","STATEMENT" buttons and their click functionality.

    
    private JButton addMoneyBtn(int width, int height) {
		  
          JButton addMoneyBtn = new JButton(new ImageIcon(((new ImageIcon("images/desk_bg_9.png")).getImage()).getScaledInstance( height*22/100, height*22/100, java.awt.Image.SCALE_SMOOTH)));
          addMoneyBtn.setContentAreaFilled(false);
          addMoneyBtn.setBorder(null);
          addMoneyBtn.setBounds(width*10/100,(int) height*2/100, height*22/100, height*26/100); 
          addMoneyBtn.setForeground(Color.black);
          addMoneyBtn.setLayout(null);
          addMoneyBtn.setOpaque(false);
          addMoneyBtn.setContentAreaFilled(false);
          

          JLabel text = new JLabel("Add Money");
          text.setHorizontalAlignment(JLabel.CENTER);
          text.setVerticalAlignment(JLabel.CENTER);
          text.setBackground(Color.decode("#80c2b2"));
          text.setForeground(Color.white);
          text.setFont(new java.awt.Font("Serif",0, 12));
          text.setBounds(0,height*18/100, height*22/100, height*5/100);
          addMoneyBtn.add(text);
          

          ImageIcon backround_img1 = new ImageIcon("images/add_money.png");
          Image img1 = backround_img1.getImage();
          Image temp_img1 = img1.getScaledInstance(height*15/100, height*15/100, Image.SCALE_SMOOTH);
          backround_img1 = new ImageIcon(temp_img1);
          JLabel background1 = new JLabel("", backround_img1, JLabel.CENTER);
          background1.setBounds((int) (height*3.8f/100),height*3/100, height*15/100, height*15/100);
          addMoneyBtn.add(background1);

          addMoneyBtn.addActionListener(new ActionListener() {
              
              @Override
              public void actionPerformed(ActionEvent e) {
                  // TODO Auto-generated method stub
                  new AddMoney(dashboardFrame);
  
              }
          });
          
          return addMoneyBtn;		
      }
      
      
      private JButton addExpenseBtn(int width, int height) {
           
          JButton addExpenseBtn = new JButton(new ImageIcon(((new ImageIcon("images/desk_bg_9.png")).getImage()).getScaledInstance( height*22/100, height*22/100, java.awt.Image.SCALE_SMOOTH)));
          addExpenseBtn.setContentAreaFilled(false);
          addExpenseBtn.setBorder(null);
          addExpenseBtn.setBounds(width*30/100,(int) height*2/100, height*22/100, height*26/100); 
          addExpenseBtn.setForeground(Color.black);
          addExpenseBtn.setLayout(null);
          addExpenseBtn.setOpaque(false);
          addExpenseBtn.setContentAreaFilled(false);
          
          JLabel text = new JLabel("Exepense");
          text.setHorizontalAlignment(JLabel.CENTER);
          text.setVerticalAlignment(JLabel.CENTER);
          text.setBackground(Color.decode("#80c2b2"));
          text.setForeground(Color.white);
          text.setFont(new java.awt.Font("Serif",0, 12));
          text.setBounds(0,height*18/100, height*22/100, height*5/100);
          addExpenseBtn.add(text);
          
          
          ImageIcon backround_img1 = new ImageIcon("images/transaction.png");
          Image img1 = backround_img1.getImage();
          Image temp_img1 = img1.getScaledInstance(height*12/100, height*12/100, Image.SCALE_SMOOTH);
          backround_img1 = new ImageIcon(temp_img1);
          JLabel background1 = new JLabel("", backround_img1, JLabel.CENTER);
          background1.setBounds((int) (height*5f/100),height*5/100, height*12/100, height*12/100);
          addExpenseBtn.add(background1);

          addExpenseBtn.addActionListener(new ActionListener() {
              
              @Override
              public void actionPerformed(ActionEvent e) {
                  // TODO Auto-generated method stub
                  
                  new Expense(dashboardFrame);
  
              }
          });
          
          return addExpenseBtn;		
      }
  
      
      private JButton statementBtn(int width, int height) {
            
          JButton statementBtn = new JButton(new ImageIcon(((new ImageIcon("images/desk_bg_9.png")).getImage()).getScaledInstance( height*22/100, height*22/100, java.awt.Image.SCALE_SMOOTH)));
          statementBtn.setContentAreaFilled(false);
          statementBtn.setBorder(null);
          statementBtn.setBounds(width*50/100,(int) height*2/100, height*22/100, height*26/100); 
          statementBtn.setForeground(Color.black);
          statementBtn.setLayout(null);
          statementBtn.setOpaque(false);
          statementBtn.setContentAreaFilled(false);
          
          JLabel text = new JLabel("Statement");
          text.setHorizontalAlignment(JLabel.CENTER);
          text.setVerticalAlignment(JLabel.CENTER);
          text.setBackground(Color.decode("#80c2b2"));
          text.setForeground(Color.white);
          text.setFont(new java.awt.Font("Serif",0, 12));
          text.setBounds(0,height*18/100, height*22/100, height*5/100);
          statementBtn.add(text);
          
          
          ImageIcon backround_img1 = new ImageIcon("images/statement.png");
          Image img1 = backround_img1.getImage();
          Image temp_img1 = img1.getScaledInstance(height*12/100, height*12/100, Image.SCALE_SMOOTH);
          backround_img1 = new ImageIcon(temp_img1);
          JLabel background1 = new JLabel("", backround_img1, JLabel.CENTER);
          background1.setBounds((int) (height*5/100),height*5/100, height*12/100, height*12/100);
          statementBtn.add(background1);

          statementBtn.addActionListener(new ActionListener() {
              
              @Override
              public void actionPerformed(ActionEvent e) {
                  // TODO Auto-generated method stub
              
                  new StatementPage(dashboardFrame);
              }
          });
          
          return statementBtn;		
      }
  
    

Here we define our pie-charts and their data sets.

    
    static void getFirstPiechart(int panelWidth, int panelHeight, JPanel panel,
			PieSectionLabelGenerator labelGenerator) {
		// Create dataset  
	    PieDataset dataset = createDataset(); 
	     
	    // Create chart  
	    chart = ChartFactory.createPieChart(  
	        "",  
	        dataset,  
	        false,   
	        false,  
	        false);
        ((PiePlot) chart.getPlot()).setLabelGenerator(labelGenerator); 

	    // Create Panel  
	    chartPanel = new ChartPanel(chart);
	    chartPanel.setBounds(panelWidth*5/100, panelHeight*25/100,panelWidth*30/100, panelHeight*40/100);
	    panel.add(chartPanel);
	}

    private static PieDataset createDataset() {  
		  
          DefaultPieDataset dataset=new DefaultPieDataset();  
          dataset.setValue("Food ",new Double(food_expense*100/100));  
          dataset.setValue("Health", health_expense);  
          dataset.setValue("Insurance", insurance_expense);  
          dataset.setValue("Travel", travel_expense);  
          dataset.setValue("Other", other_expense);  
          return dataset; 
    
    }

    static void getSecondPiechart(int panelWidth, int panelHeight, JPanel panel,
			PieSectionLabelGenerator labelGenerator) {
		PieDataset dataset1 = createDataset1(); 
	    
	    // Create chart  
	    chart1 = ChartFactory.createPieChart("", 
	        dataset1,  
	        false,   
	        false,  
	        true);  
	    ((PiePlot) chart1.getPlot()).setLabelGenerator(labelGenerator); 

	    // Create Panel  
	    chartPanel1 = new ChartPanel(chart1);
	    chartPanel1.setBounds(panelWidth*35/100, panelHeight*25/100,panelWidth*30/100, panelHeight*40/100);
	    panel.add(chartPanel1);
	}

    private static PieDataset createDataset1() {  
		  
          DefaultPieDataset dataset1=new DefaultPieDataset();  
          dataset1.setValue("Credit ",total_credit);  
          dataset1.setValue("Expense", total_expense);  
           
          return dataset1; 
          
    }

    static void getThirdPiechart(int panelWidth, int panelHeight, JPanel panel,
			PieSectionLabelGenerator labelGenerator) {
		PieDataset dataset2 = createDataset2(); 
	    
	    // Create chart  
	    chart2 = ChartFactory.createPieChart("",  
	        dataset2,  
	        false,   
	        false,  
	        true);  
	    ((PiePlot) chart2.getPlot()).setLabelGenerator(labelGenerator); 

	    // Create Panel  
	    chartPanel2 = new ChartPanel(chart2);
	    chartPanel2.setBounds(panelWidth*65/100, panelHeight*25/100,panelWidth*30/100, panelHeight*40/100);
	    panel.add(chartPanel2);
	}

    private static PieDataset createDataset2() {  

          DefaultPieDataset dataset2=new DefaultPieDataset();  
          dataset2.setValue("Credit ",total_credit);  
          dataset2.setValue("Expense", total_expense);  
          dataset2.setValue("Balance", total_bal);  
          return dataset2; 
    }

    

After we fetch data from database for our piecharts.

    
    static void get_account_table_data() {
		  
          int old_balance = 0;
          int old_credit = 0;
          int old_debit = 0;
          
          
          conn = SqliteConnection.ConnectDb();
          try {
              insert = conn.prepareStatement("SELECT balance FROM statement where user_id = ?");
              insert.setString(1, AppConstant.USER_ID);
              ResultSet rs = insert.executeQuery();
               
               while (rs.next()) {   
                   int value = rs.getInt(1);
                   old_balance += value;  
                  }
              
               total_bal = old_balance;
  
               insert = conn.prepareStatement("SELECT debit FROM statement where user_id = ?");
               insert.setString(1, AppConstant.USER_ID);
               ResultSet rs1 = insert.executeQuery();
               while (rs1.next()) {
                   int value = rs1.getInt(1);
                   old_debit += value;  
                  }
              
               total_expense = old_debit;
  
               
               insert = conn.prepareStatement("SELECT credit FROM statement where user_id = ?");
               insert.setString(1, AppConstant.USER_ID);
               ResultSet rs2 = insert.executeQuery();
               while (rs2.next()) {
                   int value = rs2.getInt(1);
                   old_credit += value;  
                  }
               total_credit = old_credit;
               
               
               if(old_credit == 0 && old_debit == 0) {
                   total_expense = 100;
                   total_credit = 100;
               }
  
               
               conn.close();
               
               
              }catch (SQLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          
      }
  
    

Here we get food data from database for our piecharts.


    static void get_food_data() {
		  
        int old_data = 0;
        String category = "Food";
          
          
        conn = SqliteConnection.ConnectDb();
        try {
            insert = conn.prepareStatement("SELECT debit FROM statement where user_id = ? and category = ?");
            insert.setString(1, AppConstant.USER_ID);
            insert.setString(2, category);
            ResultSet rs = insert.executeQuery();
               
                while (rs.next()) { 
                    int value = rs.getInt(1);
                    old_data += value;  
                }
               
            food_expense = old_data;
               
            conn.close();
               
            }catch (SQLException e) {
            // TODO Auto-generated catch block
             e.printStackTrace();
          }   
      }
  

    

Here we get health data from database for our piecharts.


    static void get_health_data() {
		  
          int old_data = 0;
          String category = "Health Care";
          
          
          conn = SqliteConnection.ConnectDb();
          try {
              insert = conn.prepareStatement("SELECT debit FROM statement where user_id = ? and category = ?");
              insert.setString(1, AppConstant.USER_ID);
              insert.setString(2, category);
              ResultSet rs = insert.executeQuery();
               
               while (rs.next()) {  
                   int value = rs.getInt(1);
                   old_data += value;  
                  }
               
               health_expense = old_data;
               
  
               conn.close();
               
              }catch (SQLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          
      }
  
    

Here we get insurance data from database for our piecharts.


    static void get_insurance_data() {
		  
          int old_data = 0;
          String category = "Insurance";
          
          
          conn = SqliteConnection.ConnectDb();
          try {
              insert = conn.prepareStatement("SELECT debit FROM statement where user_id = ? and category = ?");
              insert.setString(1, AppConstant.USER_ID);
              insert.setString(2, category);
              ResultSet rs = insert.executeQuery();
               
               while (rs.next()) {
                   int value = rs.getInt(1);
                   old_data += value;  
                  }
               
               insurance_expense = old_data;
               
               conn.close();
               
               
              }catch (SQLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          
      }
    

Here we get travel data from database for our piecharts.


    static void get_travel_data() {
		  
          int old_data = 0;
          String category = "Travel";
          
          
          conn = SqliteConnection.ConnectDb();
          try {
              insert = conn.prepareStatement("SELECT debit FROM statement where user_id = ? and category = ?");
              insert.setString(1, AppConstant.USER_ID);
              insert.setString(2, category);
              ResultSet rs = insert.executeQuery();
               
               while (rs.next()) {   
                   int value = rs.getInt(1);
                   old_data += value;  
                  }
               
               travel_expense = old_data;
               conn.close();
               
               
              }catch (SQLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          
      }
      
    

Here we get other data from database for our piecharts.


    static void get_other_data() {
		  
          int old_data = 0;
          String category = "Other";
          
          
          conn = SqliteConnection.ConnectDb();
          try {
              insert = conn.prepareStatement("SELECT debit FROM statement where user_id = ? and category = ?");
              insert.setString(1, AppConstant.USER_ID);
              insert.setString(2, category);
              ResultSet rs = insert.executeQuery();
               
               while (rs.next()) { 
                   int value = rs.getInt(1);
                   old_data += value;  
                  }
               
               other_expense = old_data;
               
               if(other_expense == 0 && food_expense == 0 && health_expense == 0 && travel_expense == 0 && insurance_expense == 0) {
                   health_expense = 0;
                   other_expense = 0;
                   food_expense = 0;
                   travel_expense = 0;
                   insurance_expense = 1;
               }
               
               conn.close();
               
               
              }catch (SQLException e) {
              // TODO Auto-generated catch block
              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.