Add Money

Add Money

Above UI we are going to create by Awt for Add Money Window 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.

    
		addMoneyFrame = new JFrame();
		Utils.centeredFrame(addMoneyFrame, windowWidth, windowHeight, "Add Money");
		addMoneyFrame.setResizable(false);
				
		Container frameContainer = addMoneyFrame.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.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 ;
		posY = windowHeight / 2 - containerHeight / 2 - frameControllerSize / 2;
		JComponent jcomponent = new RoundedBackground(posX, posY, containerWidth, containerHeight);
		jcomponent.setLayout(null);
		frameContainer.add(jcomponent, BorderLayout.CENTER);

    

Here we create a Label for our heading.

    
		// for heading Layout
		posY = panelHeight * 2/100;
		Font headingFont = new Font("Serif", Font.PLAIN, 22);
		// header
									
		JLabel heading_text = new JLabel("Add Money");
		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 / 15);
		panel.add(heading_text);
					
		panel.add(Utils.getSeparator(0, panelHeight / 13, panelWidth, 5,"000000"));			

	

Here we create Action Panel and its UI.

    

		// Action table Base panel
		Font textFont = new Font("Serif",1,12);
		Border borderline = BorderFactory.createLineBorder(Color.black);

		int actionPanelWidth = panelWidth*35/100;
		int actionPanelHeight = panelHeight*86/100;
	    actionPanel = new JPanel();
	    actionPanel.setBorder(Utils.getTitledBorder(10, "Actions"));
	    actionPanel.setBounds(0,panelHeight*10/100, actionPanelWidth, actionPanelHeight);
	    actionPanel.setLayout(null);
	    panel.add(actionPanel);
	   
	    //Ammount Label
	    textFont = new Font("Serif",0,12);
	    JLabel amountLabel = new JLabel("Amount");
	    amountLabel.setAlignmentX(amountLabel.LEFT_ALIGNMENT);
	    amountLabel.setAlignmentY(amountLabel.LEFT_ALIGNMENT);
	    amountLabel.setForeground(Color.black);
	    amountLabel.setFont(textFont);
	    amountLabel.setBounds(actionPanelWidth*15/100,actionPanelHeight*10/100, actionPanelWidth*70/100, actionPanelHeight*7/100);
	    amountLabel.setBackground(Color.black);
	    actionPanel.add(amountLabel);
	    
		//Text Field for enter ammount
	    amountField = new JTextField();
	    amountField.setForeground(Color.black);
	    amountField.setBounds(actionPanelWidth*15/100,actionPanelHeight*12/100, actionPanelWidth*70/100, actionPanelHeight*10/100);
	    amountField.setOpaque(false);
	    amountField.setBorder(BorderFactory.createLineBorder(Color.white, 0));
	    amountField.setBackground(null);
	    actionPanel.add(amountField);
	    actionPanel.add(Utils.getSeparator(actionPanelWidth*15/100,actionPanelHeight*20/100,actionPanelWidth*70/100,5,"000000"));
	    
		//Description Label
	    JLabel descriptionLabel = new JLabel("Description");
	    descriptionLabel.setAlignmentX(descriptionLabel.LEFT_ALIGNMENT);
	    descriptionLabel.setAlignmentY(descriptionLabel.LEFT_ALIGNMENT);
	    descriptionLabel.setForeground(Color.black);
	    descriptionLabel.setFont(textFont);
	    descriptionLabel.setBounds(actionPanelWidth*15/100,actionPanelHeight*23/100, actionPanelWidth*70/100, actionPanelHeight*7/100);
	    descriptionLabel.setBackground(Color.black);
	    actionPanel.add(descriptionLabel);
	    
		//Text Area for enter description
	    descriptionArea = new JTextArea();  
	    descriptionArea.setBounds(actionPanelWidth*15/100,actionPanelHeight*30/100, actionPanelWidth*70/100,actionPanelHeight*30/100);  
        actionPanel.add(descriptionArea);  

	    addMoneyBtn(actionPanelWidth, actionPanelHeight);

         

Here we Define "ADD MONEY" Button and its click.

    
	private void addMoneyBtn(int width, int height) {
		
		addMoneyBtn = new JButton(new ImageIcon(((new ImageIcon("images/button38.png")).getImage()).getScaledInstance(width*60/100, height*10/100, java.awt.Image.SCALE_SMOOTH)));
		addMoneyBtn.setContentAreaFilled(false);
		addMoneyBtn.setBorder(null);
		addMoneyBtn.setText("ADD MONEY");
		addMoneyBtn.setHorizontalTextPosition(JButton.CENTER);
		addMoneyBtn.setVerticalTextPosition(JButton.CENTER);
		addMoneyBtn.setBounds(width*20/100,height*80/100, width*60/100, height*10/100); 
		addMoneyBtn.setOpaque(false);
		addMoneyBtn.setContentAreaFilled(false);
		addMoneyBtn.setBorderPainted(false);
		addMoneyBtn.setFont(new java.awt.Font("Serif",1, 15));
		addMoneyBtn.setForeground(Color.white);
		actionPanel.add(addMoneyBtn);
			
		addMoneyBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
	
				String var = "";
				String amount = (amountField.getText());
				String description = descriptionArea.getText();
				String credit_date = java.time.LocalDate.now()+"";
				//String debit_date = date+"";
				String user_id = AppConstant.USER_ID;
				int  balance = 0;
				int old_credit = 0;
				int old_debit = 0;
				
				boolean is_amount_entered = false;
				boolean is_descrip_entered = false;
				
				if(amount.isEmpty()) {
					var += "Amount Should not be empty, ";
		        }else {
		        	is_amount_entered = true;
		        }
				
				if(description.isEmpty()) {
					var += "Description Should not be empty, ";
		        }else {
		        	is_descrip_entered = true;
		        }
			
				
				if(is_amount_entered && is_descrip_entered) {
				conn = SqliteConnection.ConnectDb();
				
				try {
					
					insert = conn.prepareStatement("SELECT credit FROM statement where user_id = ?");
					insert.setString(1, user_id);
					ResultSet rs = insert.executeQuery();
					 
					 while (rs.next()) { 
						 int value = rs.getInt(1);
						 old_credit += value;
				        }
					 
					 insert = conn.prepareStatement("SELECT debit FROM statement where user_id = ?");
						insert.setString(1, user_id);
						ResultSet rs1 = insert.executeQuery();
						 
						 while (rs1.next()) { 
							 int value = rs1.getInt(1);
							 old_debit += value;
					        } 
					 
						 int entered_amount = Integer.parseInt(amount);
						 balance = (old_credit - old_debit) + (entered_amount);	
						 
						 String debit = "";
						 String debit_date = "";
						 
						insert = conn.prepareStatement("insert into statement(user_id, credit, debit, balance, credit_date, debit_date, description)values(?,?,?,?,?,?,?)");
						insert.setString(1, user_id);
						insert.setInt(2, entered_amount);
						insert.setString(3, debit);
						insert.setInt(4, balance);
						insert.setString(5, credit_date);
						insert.setString(6, debit_date);
						insert.setString(7, description);
						insert.executeUpdate();	 
						 
						
						conn.close();

						amountField.setText("");
						descriptionArea.setText("");
						data_table_update();
						 
					
				} catch (SQLException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}

				var = "Success credited successfully";
				JOptionPane.showMessageDialog(null, var);
				
			}else {
				JOptionPane.showMessageDialog(null, var);
			}
					
			}
		});
	}
     

Here we create History Panel and its UI.

    
		// History Table Base panel
	
		int historyPanelWidth = panelWidth*64/100;
		int historyPanelHeight = panelHeight*86/100;
		
	    historyPanel = new JPanel();
	    historyPanel.setLayout(new FlowLayout());
	    historyPanel.setLayout(null);
	    historyPanel.setBounds(panelWidth*36/100,panelHeight*10/100, historyPanelWidth, historyPanelHeight);
	    historyPanel.setBorder(Utils.getTitledBorder(10, "History"));
	    panel.add(historyPanel);
	    
	    headingFont = new Font("Serif",Font.BOLD,16);
	    JLabel user_name_L = new JLabel("Name: "+ AppConstant.USER_NAME);
	    user_name_L.setAlignmentX(JLabel.LEFT);
	    user_name_L.setForeground(Color.black);
	    user_name_L.setFont(headingFont);
	    user_name_L.setBounds(historyPanelWidth*2/100,historyPanelHeight*2/100, historyPanelWidth*40/100, historyPanelHeight*10/100);
	    historyPanel.add(user_name_L);
	   
	    data_table(historyPanelWidth, historyPanelHeight);

	

Here we define Data Table and its data updation.

    
	private void data_table(int width, int height) {		
		
		// TODO Auto-generated method stub
		String[] columnNames = {"Credit",
                "Credit Date",
                "Balance",
                "Description"
               	};
		
		DefaultTableModel model1 = new DefaultTableModel();
		sorter2 = new TableRowSorter<>(model1);

        model1.setColumnIdentifiers(columnNames); 
		
        dataTable = new JTable(){
			public boolean isCellEditable(int row, int column) {                
                return false;               
			};
		};
        dataTable.setBounds(width*2/100,height*15/100, width*96/100,height*80/100);
        dataTable.setRowSorter(sorter2);
		historyPanel.add(dataTable);
		dataTable.setModel(model1);

		JScrollPane scrollPane1 = new JScrollPane(dataTable);
		scrollPane1.setBounds(width*2/100,height*15/100, width*96/100,height*80/100);
		dataTable.setFillsViewportHeight(true);
		historyPanel.add(scrollPane1);
		
		DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
		centerRenderer.setHorizontalAlignment( JLabel.CENTER );
		dataTable.getColumnModel().getColumn(0).setCellRenderer( centerRenderer );
		dataTable.getColumnModel().getColumn(1).setCellRenderer( centerRenderer );
		dataTable.getColumnModel().getColumn(2).setCellRenderer( centerRenderer );
		dataTable.getColumnModel().getColumn(3).setCellRenderer( centerRenderer );
		
	
		dataTable.getTableHeader().setFont(new Font("SansSerif", 1, 15));
		dataTable.getTableHeader().setBackground(Color.decode("#980AF2"));
		dataTable.getTableHeader().setForeground(Color.decode("#FFFFFF"));
		dataTable.getTableHeader().setPreferredSize(new Dimension(760, 35));
		dataTable.setRowHeight(25);

		dataTable.getColumnModel().getColumn(0).setPreferredWidth(30);
		dataTable.getColumnModel().getColumn(3).setPreferredWidth(100);
		
		dataTable.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
	        public void valueChanged(ListSelectionEvent event) {
	           
	        }
	    });

		data_table_update();
	
	}

	//Data of Table updation
	private void data_table_update() {		
		
		conn = SqliteConnection.ConnectDb();
		try {
			insert = conn.prepareStatement("SELECT credit, credit_date, balance, description FROM statement WHERE user_id = ?");
			insert.setString(1, AppConstant.USER_ID);
			ResultSet rs = insert.executeQuery();
			
			DefaultTableModel dft = (DefaultTableModel)dataTable.getModel();
			dft.setRowCount(0);
			
			while(rs.next()) {
				Vector v2 = new Vector();
	
					v2.add(rs.getString("credit"));
					v2.add(rs.getString("credit_date"));
					v2.add(rs.getString("balance"));
					v2.add(rs.getString("description"));
			
				dft.addRow(v2);
				
			}
			
			conn.close();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}
	

At last we will manage closing of window

    
    	addMoneyFrame.addWindowListener(new WindowAdapter(){  
            public void windowClosing(WindowEvent e) {  
            	addMoneyFrame.dispose();
            	dashboardFrame.setVisible(true);
            	
            	DashboardNew.get_account_table_data();

            	DashboardNew.get_food_data();
            	DashboardNew.get_health_data();
                DashboardNew.get_insurance_data();
                DashboardNew.get_travel_data();
                DashboardNew.get_other_data();
                

                if (null != DashboardNew.chartPanel && null != DashboardNew.chartPanel1 && null != DashboardNew.chartPanel2) {
                	DashboardNew.panel.remove(DashboardNew.chartPanel);
                	DashboardNew.panel.remove(DashboardNew.chartPanel1);
                	DashboardNew.panel.remove(DashboardNew.chartPanel2);
                	
                	
                	PieSectionLabelGenerator labelGenerator = new StandardPieSectionLabelGenerator(  
            	            " {0} : {2}", new DecimalFormat("0"), new DecimalFormat("0%")); 
            	    
            	    
                	DashboardNew.getFirstPiechart(panelWidth, panelHeight, DashboardNew.panel, labelGenerator);
            	    
            	    
                	DashboardNew.getSecondPiechart(panelWidth, panelHeight, DashboardNew.panel, labelGenerator);
            	    
            	    
                	DashboardNew.getThirdPiechart(panelWidth, panelHeight, DashboardNew.panel, labelGenerator);
                }
            }
        });  
                       

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.