Search code examples
javaswingjtextfieldjpasswordfield

Adding a password field - GUI


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


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();
             }
          });
       }
    }