You are reading the article Declaration, Method Detail Of Jpasswordfield With Example updated in September 2023 on the website Lifecanntwaitvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Declaration, Method Detail Of Jpasswordfield With Example
Introduction to JPasswordFieldWeb development, programming languages, Software testing & others
Declaration of Jpasswordfield:
public class JPasswordField extends JTextFieldAs you can see, this class is a subclass of JTextField, it inherits all the properties of a text field plus its own functionalities like masking the entered characters.
Constructor Details
JPasswordField(): This is the simplest constructor that creates a password field with default document, zero column width and no (null) starting text string.
JPasswordField(Document doc, String txt, int columns): This constructor creates a password field with a specified document, specified column width, and specified default password.
JPasswordField(int columns): This constructor creates a password field with specified column width.
JPasswordField(String text): This constructor creates a password field with the specified default password.
JPasswordField(String text, int columns): This constructor creates a password field with specified column width and specified default password.
Method details
char[] getPassword(): This is an important and most useful method of JPasswordField class which returns the password, as a character array, entered in this JPasswordField.
String getText(): This method returns the password, as a string, entered in this JPasswordField. But this method is deprecated in Java 2 platform v1.2, replaced by the getPassword() method.
String getText(int offs, int len): This method returns a portion of the password, as a string, entered in this JPasswordField. But this method is deprecated in Java 2 platform v1.2, replaced by getPassword() method.
void copy(): This method invokes provideErrorFeedback on the current look and feel which initiates an error beep.
void cut(): This method invokes provideErrorFeedback on the current look and feels which initiates an error beep.
boolean echoCharIsSet(): This method returns true if a character is set for echoing to this JPasswordField. Otherwise false.
char getEchoChar(): This method returns the character which is set to this JPasswordField for echoing.
void setEchoChar(char c): This method sets the echo character to this password field.
String getUIClassID(): This method returns the name of the look and feel class that renders this component.
protected String paramString(): This method returns a string representation of this password field.
AccessibleContext getAccessibleContext(): This method returns the AccessibleContext associated with this JPasswordField.
Example of JpasswordfieldBelow is the example for Jpasswordfield:
Code:
package application; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class JPasswordFieldDemo { public static void main(String[] args) { JFrame app = new JFrame("JPasswordField Demo"); app.setLayout(new GridLayout(4, 0)); app.setSize(400, 400); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); JPanel panel3 = new JPanel(); JPanel panel4 = new JPanel(); JLabel label = new JLabel("Password : "); JPasswordField passwordField = new JPasswordField(10); JButton submit = new JButton("submit"); panel1.add(label); panel1.add(passwordField); panel1.add(submit); JLabel status = new JLabel("Password is : "); panel2.add(status); JLabel label2 = new JLabel("Set echo character : "); JTextField textFieldForEchoChar = new JTextField(5); JButton setEchoCharButton = new JButton("SetEchoChar"); JLabel statusOfSetEchoChar = new JLabel("Echo character is : " + passwordField.getEchoChar()); panel3.add(label2); panel3.add(textFieldForEchoChar); panel3.add(setEchoCharButton); panel4.add(statusOfSetEchoChar); app.add(panel1); app.add(panel2); app.add(panel3); app.add(panel4); app.setVisible(true); ActionListener actionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String command = e.getActionCommand();if (command.equals("submit")) { String password = new String(passwordField.getPassword()); status.setText("Password is : " + password); } if (command.equals("SetEchoChar")) { char echoChar = textFieldForEchoChar.getText().charAt(0); passwordField.setEchoChar(echoChar); statusOfSetEchoChar.setText("Echo character set to: " + echoChar); } } }; submit.addActionListener(actionListener); setEchoCharButton.addActionListener(actionListener); } }Explanation:
This is a simple application in which JPasswordField and its functionalities are shown.
At the top, there is a JFrame which is our application.
In this JFrame four panels are added.
In the first panel, a JPasswordField is added so that the user can enter the password here.
And a button is added to submit the password.
Status in the second panel is updated using an action listener which is added to the submit button.
When the user types the password, it is not shown in the JPasswordField, instead, echo chars are shown, and the password is hidden.
There is a default echo char. Also, we can set the echo char for the password field. Now we are going to do that.
In the third panel, a text box and a button are added. This text box is to take echo41char from the user.
Users will have to enter a character and press the button and echo char will be set to the password field.
In the fourth panel, the user can see the current echo char or newly set echo char of the password field. This is achieved using a JLabel and an action listener added to the button in the third panel.
1. This is the application in which the user can enter the password, see the entered password, change the echo char of the password field and see the current echo char:
4. Similarly, the user sets the echo char to “#”:
Conclusion – JpasswordfieldJPasswordField is an important component in Swing which enables the user to enter the password without visually disposing it to others. This file can be used to develop a login and sign up page of any application. This increases the security of the application and makes them look and feel better. Also, the developer can set his favorite echo char!
Recommended ArticlesThis is a guide to Jpasswordfield. Here we discuss the Jpasswordfield can be used to develop a log in and sign up page of any application. You may also look at the following article to learn more –
You're reading Declaration, Method Detail Of Jpasswordfield With Example
Update the detailed information about Declaration, Method Detail Of Jpasswordfield With Example on the Lifecanntwaitvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!