Table of contents
1.
Introduction
2.
Class Declaration
3.
Jpasswordfield Class constructors
4.
Jpasswordfield Class Methods
5.
Methods Inherited
6.
Example
7.
Frequently Asked Questions
7.1.
What is Jpasswordfield?
7.2.
What is the use of set echo char method?
7.3.
What is getPassword in Java?
7.4.
What is JTextArea in Java?
7.5.
Which method is used to change the default character of JPasswordField?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

Jpasswordfield

Author Alok Pandey
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JPasswordField is a little component that enables modification of a single line of text without displaying the original characters, even while the view shows that anything was typed.

JPasswordField is considered to be source-compatible with java.awt.TextField used with echoChar set TextField with echoChar set is utilized. It is offered separately to make it simpler and safer to modify the JTextField's user interface without affecting password entries.

Jpasswordfield

Typical usage of JPasswordField looks like this:

// create new object
JPasswordField passwordField = new JPasswordField(20);

// add to the container
frame.add(passwordField);

// get the password
char[] password = passwordField.getPassword();

Class Declaration

Declaration for javax.swing.JPasswordField class:

public class JPasswordField
extends JTextField
You can also try this code with Online Java Compiler
Run Code

Jpasswordfield Class constructors

Here are the Jpasswordfield Class constructors.

Constructor

Description

JPasswordField() 

Constructs a new JPasswordField, with a default document, null starting text string, and 0 column width.

JPasswordField(Document doc, String txt, int columns)

Constructs a new JPasswordField that uses the given text storage model and the given number of columns.

JPasswordField(int columns)

Constructs a new empty JPasswordField with the specified number of columns.

 

JPasswordField(String text)

Constructs a new JPasswordField initialized with the specified text.

JPasswordField(String text, int columns)

Constructs a new JPasswordField initialized with the specified text and columns.

 

Jpasswordfield Class Methods

Here are some of the widely used Jpasswordfield Class constructors.

Class Methods

Description

char getEchoChar()

Returns the character to be used for echoing.

setEchoChar(char c)

set the echo character for JPasswordField.

char[] getPassword()

Returns the text contained in this TextComponent.

String getText(int offs, int len)

Deprecated. As of Java 2 platform v1.2, replaced by getPassword

String getText()

Deprecated. As of Java 2 platform v1.2, replaced by getPassword.

void setEchoChar(char c)

Sets the echo character for this JPasswordField

void updateUI()

This Reloads the pluggable UI.

protected String paramString()

Returns a string representation of this JPasswordField.

 

String getUIClassID() 

Returns the name of the L&F class that renders this component.

boolean echoCharIsSet()

Returns true if this JPasswordField has a character set for echoing

Methods Inherited

  • java.lang.Object
  • javax.swing.JTextField
  • java.awt.Container
  • javax.swing.JComponent
  • javax.swing.text.JTextComponent
  • java.awt.Component

Example

Program for entering name and password using JTextField and JPasswordField.

package com.codingninjas.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingControlDemo {
	private JFrame mainFrame;
	private JLabel headerLabel;
	private JLabel statusLabel;
	private JPanel controlPanel;
	
	public SwingControlDemo(){
		prepareGUI();
	}

	public static void main(String[] args){
		SwingControlDemo  swingControlDemo = new SwingControlDemo();      
		swingControlDemo.showPasswordFieldDemo();
	}

	private void prepareGUI(){
		mainFrame = new JFrame("Jpasswordfield Example");
		mainFrame.setSize(400,400);
		mainFrame.setLayout(new GridLayout(3, 1));

		mainFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent windowEvent){
				System.exit(0);
			}
		});

		headerLabel = new JLabel("", JLabel.CENTER);        
		statusLabel = new JLabel("",JLabel.CENTER);    
		statusLabel.setSize(350,100);

		controlPanel = new JPanel();
		controlPanel.setLayout(new FlowLayout());

		mainFrame.add(headerLabel);
		mainFrame.add(controlPanel);
		mainFrame.add(statusLabel);
		mainFrame.setVisible(true);
	}

	private void showPasswordFieldDemo(){
		headerLabel.setText("JPasswordField"); 
		JLabel namelabel= new JLabel("Email ID: ", JLabel.RIGHT);
		JLabel passwordLabel = new JLabel("Password: ", JLabel.CENTER);
		final JTextField userText = new JTextField(6);
		final JPasswordField passwordText = new JPasswordField(6);      
		passwordText.setEchoChar('~');
  
		JButton loginButton = new JButton("Submit");
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {     
				String data = "Username " + userText.getText();
				data += ", Password: " + new String(passwordText.getPassword()); 
				statusLabel.setText(data);        
			}
		});
		
		controlPanel.add(namelabel);
		controlPanel.add(userText);
		controlPanel.add(passwordLabel);       
		controlPanel.add(passwordText);
		controlPanel.add(loginButton);
		mainFrame.setVisible(true);  
	}
}

 

OUTPUT

Frequently Asked Questions

What is Jpasswordfield?

JPasswordField is a little component that enables modification of a single line of text without displaying the original characters, even while the view shows that anything was typed.

JPasswordField is considered to be source-compatible with java.awt.TextField used with echoChar set TextField with echoChar set is utilized.

What is the use of set echo char method?

You can specify a character to display each time a user types a password into the JPasswordField using the echo char command.

What is getPassword in Java?

char[] getPassword() Returns the password as a character array.

What is JTextArea in Java?

A multi-line area that shows plain text is known as a JTextArea. Where it is reasonable to do so, it is designed to be a small component that offers source compatibility with the java. awt. TextArea class.

Which method is used to change the default character of JPasswordField?

The recommended field size, which in this case is at least 10 columns wide, is indicated by the argument supplied to the JPasswordField function Object() { [native code] }. For each character entered, a dot is displayed by default in a password field. Call the setEchoChar method to modify the echo character.

Conclusion

In this article, we have extensively discussed Jpasswordfield

We hope that this blog has helped you enhance your knowledge regarding Jpasswordfield and if you would like to learn more, check out our articles on

 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. 

Enroll in our courses and refer to the mock test and problems available.

Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass