Add User

Add User

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

    
		addUserFrame = new JFrame();
		Utils.centeredFrame(addUserFrame, windowWidth, windowHeight, "Add User");
		addUserFrame.setResizable(false);
				
		Container frameContainer = addUserFrame.getContentPane();
		frameContainer.setBackground(Color.gray);

		// Changeable According to What size of screen You Want?
		int containerWidth = windowWidth*50/100;
		int containerHeight = windowHeight*90/100;
				
		int panelWidth = containerWidth*95/100;
		int panelHeight = containerHeight*90/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 Labels and Text Fields.

    
		// for heading Layout
		posY = panelHeight * 2/100;
		Font headingFont = new Font("Serif", Font.PLAIN, 22);
				
		// header
		JLabel heading_text = new JLabel("Add User");
		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"));			


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

		int actionPanelWidth = panelWidth*85/100;
		int actionPanelHeight = panelHeight*86/100;
	    actionPanel = new JPanel();
	    actionPanel.setBorder(Utils.getTitledBorder(10, "Actions"));
	    actionPanel.setBounds(panelWidth/2-actionPanelWidth/2,panelHeight*10/100, actionPanelWidth, actionPanelHeight);
	    actionPanel.setLayout(null);
	    panel.add(actionPanel);

		//Name Label
		textFont = new Font("Serif",0,12);
	    JLabel nameLabel = new JLabel("Name");
	    nameLabel.setAlignmentX(nameLabel.LEFT_ALIGNMENT);
	    nameLabel.setAlignmentY(nameLabel.LEFT_ALIGNMENT);
	    nameLabel.setForeground(Color.black);
	    nameLabel.setFont(textFont);
	    nameLabel.setBounds(actionPanelWidth*15/100,actionPanelHeight*10/100, actionPanelWidth*70/100, actionPanelHeight*7/100);
	    nameLabel.setBackground(Color.black);
	    actionPanel.add(nameLabel);
	    
		//Text Field for enter Name
	    nameField = new JTextField();
	    nameField.setForeground(Color.black);
	    nameField.setBounds(actionPanelWidth*15/100,actionPanelHeight*12/100, actionPanelWidth*70/100, actionPanelHeight*10/100);
	    nameField.setOpaque(false);
	    nameField.setBorder(BorderFactory.createLineBorder(Color.white, 0));
	    nameField.setBackground(null);
	    actionPanel.add(nameField);
	    
	    actionPanel.add(Utils.getSeparator(actionPanelWidth*15/100,actionPanelHeight*20/100,actionPanelWidth*70/100,5,"000000"));
	    
	    //Email Label
	    JLabel emailLabel = new JLabel("Email Id");
	    emailLabel.setAlignmentX(emailLabel.LEFT_ALIGNMENT);
	    emailLabel.setAlignmentY(emailLabel.LEFT_ALIGNMENT);
	    emailLabel.setForeground(Color.black);
	    emailLabel.setFont(textFont);
	    emailLabel.setBounds(actionPanelWidth*15/100,actionPanelHeight*23/100, actionPanelWidth*70/100, actionPanelHeight*7/100);
	    emailLabel.setBackground(Color.black);
	    actionPanel.add(emailLabel);
	    
		//Text field for enter Email
	    emailField = new JTextField();
	    emailField.setForeground(Color.black);
	    emailField.setBounds(actionPanelWidth*15/100,actionPanelHeight*25/100, actionPanelWidth*70/100, actionPanelHeight*10/100);
	    emailField.setOpaque(false);
	    emailField.setBorder(BorderFactory.createLineBorder(Color.white, 0));
	    emailField.setBackground(null);
	    actionPanel.add(emailField);
	    
	    actionPanel.add(Utils.getSeparator(actionPanelWidth*15/100,actionPanelHeight*33/100,actionPanelWidth*70/100,5,"000000"));
	    
		//Contact Number Label
	    JLabel contactLabel = new JLabel("Contact Number");
	    contactLabel.setAlignmentX(contactLabel.LEFT_ALIGNMENT);
	    contactLabel.setAlignmentY(contactLabel.LEFT_ALIGNMENT);
	    contactLabel.setForeground(Color.black);
	    contactLabel.setFont(textFont);
	    contactLabel.setBounds(actionPanelWidth*15/100,actionPanelHeight*36/100, actionPanelWidth*70/100, actionPanelHeight*7/100);
	    contactLabel.setBackground(Color.black);
	    actionPanel.add(contactLabel);
	    
		//Text Field for enter contact number
	    contactField = new JTextField();
	    contactField.setForeground(Color.black);
	    contactField.setBounds(actionPanelWidth*15/100,actionPanelHeight*38/100, actionPanelWidth*70/100, actionPanelHeight*10/100);
	    contactField.setOpaque(false);
	    contactField.setBorder(BorderFactory.createLineBorder(Color.white, 0));
	    contactField.setBackground(null);
	    actionPanel.add(contactField);
	    
	    actionPanel.add(Utils.getSeparator(actionPanelWidth*15/100,actionPanelHeight*46/100,actionPanelWidth*70/100,5,"000000"));
	    
	    //Password Label
	    JLabel passwordLabel = new JLabel("Enter Password");
	    passwordLabel.setAlignmentX(passwordLabel.LEFT_ALIGNMENT);
	    passwordLabel.setAlignmentY(passwordLabel.LEFT_ALIGNMENT);
	    passwordLabel.setForeground(Color.black);
	    passwordLabel.setFont(textFont);
	    passwordLabel.setBounds(actionPanelWidth*15/100,actionPanelHeight*49/100, actionPanelWidth*70/100, actionPanelHeight*7/100);
	    passwordLabel.setBackground(Color.black);
	    actionPanel.add(passwordLabel);
	    
		//textfield for enter password
	    passwordField = new JTextField();
	    passwordField.setForeground(Color.black);
	    passwordField.setBounds(actionPanelWidth*15/100,actionPanelHeight*51/100, actionPanelWidth*70/100, actionPanelHeight*10/100);
	    passwordField.setOpaque(false);
	    passwordField.setBorder(BorderFactory.createLineBorder(Color.white, 0));
	    passwordField.setBackground(null);
	    actionPanel.add(passwordField);

		actionPanel.add(Utils.getSeparator(actionPanelWidth*15/100,actionPanelHeight*59/100,actionPanelWidth*70/100,5,"000000"));

		//add button
		addUserBtn(actionPanelWidth, actionPanelHeight);  
		addUserFrame.setVisible(true);

         

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

    
	private void addUserBtn(int width, int height) {
		
		addUserBtn = new JButton(new ImageIcon(((new ImageIcon("images/button38.png")).getImage()).getScaledInstance(width*60/100, height*10/100, java.awt.Image.SCALE_SMOOTH)));
		addUserBtn.setContentAreaFilled(false);
		addUserBtn.setBorder(null);
		addUserBtn.setText("ADD USER");
		addUserBtn.setHorizontalTextPosition(JButton.CENTER);
		addUserBtn.setVerticalTextPosition(JButton.CENTER);
		addUserBtn.setBounds(width*20/100,height*75/100, width*60/100, height*10/100); 
		addUserBtn.setOpaque(false);
		addUserBtn.setContentAreaFilled(false);
		addUserBtn.setBorderPainted(false);
		addUserBtn.setFont(new java.awt.Font("Serif",1, 15));
		addUserBtn.setForeground(Color.white);
		actionPanel.add(addUserBtn);
	    
		
		addUserBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
			
				String var = "";
				String user_name = (nameField.getText());
				String user_email_id = emailField.getText();
				String user_contact_number = contactField.getText();
				String user_password = passwordField.getText();
				int id_count = 0;
				boolean is_name_entered = false;
				boolean is_email_id_entered = false;
				boolean is_contact_number_entered = false;
				boolean is_password_entered = false;
				String userId = "USR";
				
				
				if(user_name.isEmpty()) {
		            var += "Name Should not be empty, ";
		        }else {
		        	is_name_entered = true;
		        }
				
				if(user_email_id.isEmpty()) {
					var += "Email Should not be empty, ";
		        }else {
		        	is_email_id_entered = true;
		        }
				
				if(user_contact_number.isEmpty()) {
					var += "Contact Should not be empty, ";
		        }else {
		        	is_contact_number_entered = true;
		        }
				
				if(user_password.isEmpty()) {
					var += "Password Should not be empty, ";
		        }else {
		        	is_password_entered = true;
		        }
				String key = "USER_LAST_COUNT";
				
				if(is_name_entered && is_email_id_entered && is_contact_number_entered && is_password_entered) {
				conn = SqliteConnection.ConnectDb();
				
				try {
					insert = conn.prepareStatement("SELECT value FROM system_setting WHERE key = 'USER_LAST_COUNT'");
					ResultSet rs = insert.executeQuery();
					 
					 while (rs.next()) { 
						 id_count = rs.getInt(1)+1;
				        }
					 DecimalFormat df = new DecimalFormat("000");
					 userId = userId+df.format(id_count);
						 
						insert = conn.prepareStatement("insert into user(user_id, user_name, contact_number, email_address, password)values(?,?,?,?,?)");
						insert.setString(1, userId);
						insert.setString(2, user_name);
						insert.setString(3, user_contact_number);
						insert.setString(4, user_email_id);
						insert.setString(5, user_password);
						insert.executeUpdate();	 
						
						insert = conn.prepareStatement("UPDATE system_setting "
				                + "SET value = ? "
				                + "WHERE key = ?");
						insert.setInt(1, id_count);
						insert.setString(2, key);
						insert.executeUpdate();
						conn.close();
						

						nameField.setText("");
						contactField.setText("");
						emailField.setText("");
						passwordField.setText("");
					
				} catch (SQLException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				
				var = "User has been added successfully";
				JOptionPane.showMessageDialog(null, var);
				
			}else {
				JOptionPane.showMessageDialog(null, var);
			}
					
			}
		});
	}
     

At last we will manage closing of window

    
		addUserFrame.addWindowListener(new WindowAdapter(){  
            public void windowClosing(WindowEvent e) {  
            	addUserFrame.dispose();
            	dashboardFrame.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.