Java Program to Create Text Area and Password Field

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.

To display the username and password :

If the name 'thomas' and password 'thomas@123' are typed in the respective field,
the expected output is "Username = thomas Password = thomas@123"
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.

  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.


Test case – Here’s the runtime output to get the username and password. For example, if user enters Name as “thomas” and Password as “thomas@123” in the text field. After clicking view button it should display “Username = thomas Password = thomas@123”.

Sanfoundry Global Education & Learning Series – Java Programs.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.