Add User

Add User

Above UI we are going to create by AWT for register user in our quiz 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.

    
        Container frameContainer = addUserFrame.getContentPane();
		frameContainer.setBackground(Color.gray);

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

		// frame without control options
		JPanel panel = new JPanel(); 
		panel.setLayout(null);//new BorderLayout()); 
		panel.setBackground(Color.WHITE); 
		panel.setSize(new Dimension(panelWidth,panelHeight)); 
		panel.setLocation(posX, posY);
		frameContainer.add(panel);
    

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

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

    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 Labels and Text Fields.

    
        // for heading Layout
		posY = panelHeight * 2/100;
		Font font = new Font("Lucida", Font.PLAIN, 48);
		JLabel label = new JLabel();
		label.setText("Add User");
		label.setFont(font);
		label.setForeground(Color.black);
		label.setSize(new Dimension(panelWidth, panelHeight / 9));
		label.setHorizontalAlignment(JLabel.CENTER);
		label.setLocation(0, posY); 
		panel.add(label);
		
        
		int labelWidth = panelWidth*90/100;
		int labelHeight = panelHeight*5/100;
		posX = panelWidth*5/100;
        font = new Font("Lucida",Font.PLAIN,14);

        
        // user Name
		posY = panelHeight *20/100;
        JLabel userName_lbl=new JLabel();
        userName_lbl.setText("User Name");
        userName_lbl.setFont(font);
        userName_lbl.setSize(new Dimension(labelWidth,labelHeight));
        userName_lbl.setLocation(posX, posY); 
        panel.add(userName_lbl);
        
        posY = panelHeight *26/100;
	    user_TF = new JTextField();
        user_TF.setSize(new Dimension(labelWidth,labelHeight));
        user_TF.setLocation(posX, posY);	
        user_TF.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
        panel.add(user_TF);
        

        // email
        posY = panelHeight *35/100;
        JLabel email_lbl=new JLabel();
        email_lbl.setText("Email id");
        email_lbl.setFont(font);
        email_lbl.setSize(new Dimension(labelWidth,labelHeight));
        email_lbl.setLocation(posX, posY); 
        panel.add(email_lbl);
        
        posY = panelHeight *41/100;
	    email_TF = new JTextField();
	    email_TF.setSize(new Dimension(labelWidth,labelHeight));
	    email_TF.setLocation(posX, posY);	
	    email_TF.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
        panel.add(email_TF);
        
        
        
        // contact
        posY = panelHeight *50/100;
        JLabel contact_lbl=new JLabel();
        contact_lbl.setText("Contact Number");
        contact_lbl.setFont(font);
        contact_lbl.setSize(new Dimension(labelWidth,labelHeight));
        contact_lbl.setLocation(posX, posY); 
        panel.add(contact_lbl);
        
        posY = panelHeight *56/100;
	    contact_TF = new JTextField();
	    contact_TF.setSize(new Dimension(labelWidth,labelHeight));
	    contact_TF.setLocation(posX, posY);	
	    contact_TF.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
        panel.add(contact_TF);
        
        
        // password
        posY = panelHeight *65/100;
        JLabel pass_lbl = new JLabel();
        pass_lbl.setText("Enter Password");
        pass_lbl.setFont(font);
        pass_lbl.setSize(new Dimension(labelWidth,labelHeight));
        pass_lbl.setLocation(posX, posY); 
        panel.add(pass_lbl);
        
        posY = panelHeight *71/100;
        pass_TF = new JPasswordField();
        pass_TF.setSize(new Dimension(labelWidth,labelHeight));
        pass_TF.setLocation(posX, posY);
        pass_TF.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK));
        panel.add(pass_TF);
        
    

Now we Create "SAVE" Button and logic to new user register in database

    
        int btnWidth = panelWidth *65/100;
        int btnHeight = panelHeight *10/100;
        posX = panelWidth / 2 - btnWidth / 2;
        posY = panelHeight *85/100;
       
        font = new java.awt.Font("Lucida",Font.PLAIN, 15);
        JButton saveBtn = new JButton("SAVE");
        saveBtn.setBounds(posX,posY, btnWidth, btnHeight); 
        saveBtn.setOpaque(false);
        saveBtn.setContentAreaFilled(false);
        saveBtn.setBorderPainted(true);
        saveBtn.setBorder(new RoundedBorder(55, "SAVE", true, Color.cyan, font)); 
        saveBtn.setForeground(Color.black);
		panel.add(saveBtn);
        
        saveBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
            	
            	String userNameStr = user_TF.getText();
            	String emailStr = email_TF.getText();
            	String contactStr = contact_TF.getText();
            	String passStr = String.valueOf(pass_TF.getPassword()); 
        	
            	if( userNameStr.isEmpty() || emailStr.isEmpty()  || contactStr.isEmpty() || passStr.isEmpty()) {
            		
            		JOptionPane.showMessageDialog(addUserFrame, "Fields should not be empty");
            	
            	} else {
            		try {
            			Connection conn = null;
            			Statement stmt = null;
            			PreparedStatement pst  = null;
            			ResultSet rs = null;
            			int action;
            			try {
            				conn = SQLiteJDBCDriverConnection.connect(); 
            				stmt = conn.createStatement();
            				String sql =  "INSERT INTO user_data (user_name, user_contact_no, user_email_id, user_password, high_score) " +
            				"VALUES (?,?,?,?,?);"; 
            				pst = conn.prepareStatement(sql);
    		                pst.setString(1, userNameStr);
    			            pst.setLong(2, Long.parseLong(contactStr));
    			            pst.setString(3, emailStr);
    			            pst.setString(4, passStr);
    			            pst.setLong(5, 0);

    			            action = pst.executeUpdate();
        		        
            				if (action>0) {
            					JOptionPane.showMessageDialog(addUserFrame, "User added successfully"); 
            					moveToLogin(addUserFrame); 
            					 	
							} else {
								JOptionPane.showMessageDialog(addUserFrame, "please Enter corrent values"); 
							}

            			} catch (SQLException e) { 
            				System.err.println("Got an exception! "); 
            				System.err.println(e.getMessage()); 
					
            			} finally { 
            				// you should release your resources here
            				if (rs != null) {
            					rs.close(); 
            				}
            				if (stmt != null) { 
            					stmt.close(); 
            				}
            				if (conn != null) { 
            					conn.close();
            				}
            			}
        			
            		} catch (SQLException e) { 
            			System.out.println(e.getMessage()); 
            		}
            	}
            }
        });
        
    

At last we will manage go to login page

    
    private void moveToLogin(Frame registerFrame) {
		Login window = new Login(screenSize);
		window.getFrame().setVisible(true);
		registerFrame.dispose();
	}
    

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.