Java Program to Create 2 Labels and Text Fields for Entering Name and Secured Password

«
»

This is a Java program to create 2 labels and text fields for entering name and secured password.

Problem Description

We have to write a program in Java such that it creates a secured login field containing a name and secured password. When the user clicks view button, the username and password will be displayed.

Expected Input and Output

For creating a secured login field, we can have the following set of input and output.

advertisement

To display the username and password :

If the name 'thomas' and password '[email protected]' are typed in the respective field,
the expected output is "Username = thomas Password = [email protected]"
Problem Solution

1. Create a frame, two labels for ‘Name’ and ‘Password’, a text field for name, a password field for password, a text field for output and a button to view the output.
2. Position the fields and button properly on the frame.
3. Associate ActionListener with the button.
4. When the button is clicked, display the username and password in the output text field.

Program/Source Code

Here is source code of the Java Program to create a secured login field. The program is successfully compiled and tested using javac compiler on Fedora 30.

advertisement
  1. /*Java Program to create a secured login field*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. class Login implements ActionListener
  6. {
  7. 	static JTextField name;
  8. 	static JPasswordField pswd; 
  9. 	static JButton view;
  10. 	static JTextField text;
  11.         //Driver function
  12. 	public static void main(String args[])
  13. 	{
  14. 		//Create a frame
  15. 		JFrame frame=new JFrame("Login");
  16. 		frame.setSize(400,400);
  17. 		frame.setLayout(null);
  18. 		frame.setBackground(Color.white);
  19. 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  20. 		//Create two labels
  21. 		JLabel l_name=new JLabel("Name :");
  22. 		JLabel l_pswd=new JLabel("Password :");
  23. 		l_name.setBounds(50,50,50,50);
  24. 		l_pswd.setBounds(20,100,80,50);
  25. 		frame.add(l_name);
  26. 		frame.add(l_pswd);
  27. 		//Create two text fields for name and password
  28. 		name=new JTextField();
  29. 		pswd=new JPasswordField();
  30. 		name.setBounds(100,50,250,50);
  31. 		pswd.setBounds(100,100,250,50);
  32. 		frame.add(name);
  33. 		frame.add(pswd);
  34. 		//Create a button
  35. 		view=new JButton("View");
  36. 		view.setBounds(150,200,100,50);
  37. 		frame.add(view);
  38. 		//Create a text field to display the username and password
  39. 		text=new JTextField();
  40. 		text.setBounds(0,250,400,50);
  41. 		frame.add(text);
  42. 		//Create an object
  43. 		Login obj=new Login();
  44. 		//Associate ActionListener with the button
  45. 		view.addActionListener(obj);
  46. 		//Display the frame
  47. 		frame.setVisible(true);
  48. 	}
  49.         //function to display the username and password typed
  50. 	public void actionPerformed(ActionEvent e)
  51. 	{
  52. 		String pass=String.valueOf(pswd.getPassword());
  53. 		text.setText("Username = "+name.getText());
  54. 		text.setText(text.getText()+" Password = "+pass);
  55. 	}
  56. }
Program Explanation

1. Create a frame with background color & specific size. frame.setBackground(Color.white); is used to create the frame background color as white. frame.setSize(400,400); is used to set the width and height of the frame.
2. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); is a default close operation for the frame.
3. Create 2 labels “Name” and “Password” by using JLabel l_name=new JLabel(“Name :”); and JLabel l_pswd=new JLabel(“Password :”);. Once label is created add it to the frame by using frame.add();.
4. By using setBounds() values we can set the position of label names.
5. Create two text fields for name and password with setBounds(int x, int y, int width, int height), where ‘x’ & ‘y’ is used to set the position of top-left corner. ‘int width’ & ‘int height’ is used to set the width and height of the exit button. Once fields are created add them to the frame by using frame.add();.
name=new JTextField(); –> Creates a textfield for name.
pswd=new JPasswordField(); –> Creates a password field for secured password.
name.setBounds(100,50,250,50); –> Sets the position of name textfield.
pswd.setBounds(100,100,250,50); –> Sets the position of password field.

6. Create a view button with a setBounds() values and add it to the frame by using frame.add();.
view=new JButton(“View”); –> Used to create the view button.
view.setBounds(150,200,100,50); –> Sets the position of button.
frame.add(view); –> It will add the button to the frame.

7. Create one more text field for output to display the username and password.
8. Login obj=new Login(); is used to create the object. Once object is created associate ActionListener with the button.
9. frame.setVisible(true); is used to display the frame.
10. String pass=String.valueOf(pswd.getPassword()); –> getPassword returns a char[] of the string typed in the password field. It is converted to String format by using function String.valueOf.
11. When the View button is clicked, it display the username and password in the output text field.
text.setText(“Username = “+name.getText()); –> It displays the name in output text field
text.setText(text.getText()+” Password = “+pass); –> It displays the password in output text field.

Runtime Test Cases

Here’s the run time test case for getting username and password from a secured login field.

advertisement

Test case – Here’s the runtime output to get the username and password. For example, if user enters Name as “thomas” and Password as “[email protected]” in the text field. After clicking view button it should display “Username = thomas Password = [email protected]”.
java-program-login-window

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn