Question

I have a JTextField. The same JTextField will be used to get both name and password of the user. So if it's getting the username, I would display what the user is typing on the Text field. But if he's inserting a password, the field should not display the password (but display * characters instead).

How can I make a JTextField hide the characters that he/she types ?

Pseudocode :

If (usernameBool!=true) {
 // Display normal text - user is able to see the password
} else {
// This is going to be a password field, so i need to not show the password to the user instead show it as ******* instead.

}

Note: I don't want to use JPasswordField for this

Was it helpful?

Solution

Again, one possible solution is to use a CardLayout to swap components:

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.*;

public class SwapFields {
   private static final String TEXT_FIELD = "text field";
   private static final String PASS_FIELD = "pass field";

   public SwapFields() {
      // TODO Auto-generated constructor stub
   }

   private static void createAndShowGui() {
      final JTextField textField = new JTextField(10);
      final JPasswordField passwordField = new JPasswordField(10);

      final CardLayout cardLayout = new CardLayout();
      final JPanel cardPanel = new JPanel(cardLayout);
      cardPanel.add(textField, TEXT_FIELD);
      cardPanel.add(passwordField, PASS_FIELD);

      JToggleButton toggleBtn = new JToggleButton("Entering Password");
      toggleBtn.addItemListener(new ItemListener() {

         @Override
         public void itemStateChanged(ItemEvent iEvt) {
            if (iEvt.getStateChange() == ItemEvent.SELECTED) {
               cardLayout.show(cardPanel, PASS_FIELD);
               passwordField.requestFocusInWindow();
            } else {
               cardLayout.show(cardPanel, TEXT_FIELD);
               textField.requestFocusInWindow();
            }
         }
      });

      JButton showNamePasswordBtn = new JButton("Show Results");
      showNamePasswordBtn.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            System.out.println("Name: " + textField.getText());

            // never do this!
            System.out.println("Pass: " + new String(passwordField.getPassword()));
         }
      });

      JPanel mainPanel = new JPanel();
      mainPanel.add(cardPanel);
      mainPanel.add(toggleBtn);
      mainPanel.add(showNamePasswordBtn);

      JFrame frame = new JFrame("SwapFields");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

OTHER TIPS

The JPasswordField has a method which is setEchoChar(). As documentation states:

Sets the echo character for this JPasswordField. ... Setting a value of 0 indicates that you wish to see the text as it is typed, similar to the behavior of a standard JTextField.

You can then use just a JPasswordField and enable the echo character (eg. '*') just for the password.

I suggest you use a JPasswordField. Then when your usernameBool is true, set it to show the characters, but otherwise hide the characters, like this:

if (usernameBool) {
     password.setEchoChar((char) 0);
} else {
     password.setEchoChar('*');
}

You can create your own JTextField which your own strategy. For example, you can keep the last character (like iOS if i remember) or don't show anything (Linux style).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top