Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9003879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:46:18+00:00 2026-06-16T00:46:18+00:00

I have a JTextField . The same JTextField will be used to get both

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:46:19+00:00Added an answer on June 16, 2026 at 12:46 am

    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();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, I have an TextField. When user click on it, DatePicker will appear (in
I have this situation, where I would like to display a number in figures.
I have a page that will display one of two options for a Struts2
I have an HTML user form. User will fill form from webpage, when user
I have a JTextField that I'm not always passing a value to, but it
I have a JTextField with some text. When I click text field the cursor
I have a JFrame with some a JTextField and a JButton. I want it
I have a program that uses three JTextField fields as the main data entry
I have the following code adding an ActionListener to a JTextField: chatInput.addMouseListener(new java.awt.event.MouseAdapter() {
I am learning Java with Swing and I have some problems with using JTextField

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.