10

How to hide characters using showInputDialog of JOption pane. For eg:JOptionPane.showInputDialog("Enter Your name:").

I want to hide characters when user is giving his name. I was in the middle of the application. If I can hide those characters, my job gets done. I do not want to use JPasswordField as it requires a form to hold that label(JPasswordField).

Carlos Gavidia-Calderon
  • 7,145
  • 9
  • 34
  • 59
user10101
  • 379
  • 1
  • 7
  • 18
  • So as the user types the characters do no appear in the text field? It just remains blank? – Adam Apr 02 '12 at 06:31
  • Just be careful you don't confuse your users. If you start typing, users expect some kind of feedback. Having a box that doesn't show any feedback may be confusing. – BlueFish Apr 08 '12 at 11:19

3 Answers3

4
public static String getName() {
    JPasswordField jpf = new JPasswordField(24);
    JLabel jl = new JLabel("Enter Your Name: ");
    Box box = Box.createHorizontalBox();
    box.add(jl);
    box.add(jpf);
    int x = JOptionPane.showConfirmDialog(null, box, "Name Entry", JOptionPane.OK_CANCEL_OPTION);

    if (x == JOptionPane.OK_OPTION) {
      return jpf.getText();
    }
    return null;
  }
Clint
  • 8,988
  • 1
  • 26
  • 40
2

Here's a solution which uses JOptionPane to display a JPasswordField which prints blank spaces as the user types.

Really all you would want to place into your code is the showPasswordPrompt() method, but the code includes a main() method which allows you to easily test what the dialog looks like.

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

public class JOptionPaneTest
{
    public static String showPasswordPrompt( Component parent, String title )
    {
        // create a new JPasswordField
        JPasswordField passwordField = new JPasswordField( );
        // display nothing as the user types
        passwordField.setEchoChar( ' ' );
        // set the width of the field to allow space for 20 characters
        passwordField.setColumns( 20 );

        int returnVal = JOptionPane.showConfirmDialog( parent, passwordField, title, JOptionPane.OK_CANCEL_OPTION );

        if ( returnVal == JOptionPane.OK_OPTION )
        {
            // there's a reason getPassword() returns a char[], but we ignore this for now...
            // see: http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords
            return new String( passwordField.getPassword( ) );
        }
        else
        {
            return null;
        }
    }

    public static void main( String[] args )
    {
        final JFrame frame = new JFrame( );
        final JButton button = new JButton( "Push Me For Dialog Box" );

        button.addActionListener( new ActionListener( )
        {
            @Override
            public void actionPerformed( ActionEvent e )
            {
                String password = showPasswordPrompt( frame, "Enter Password:" );

                button.setText( password );
            }
        } );

        frame.add( button );
        frame.setSize( 400, 400 );
        frame.setVisible( true );
    }
}
ulmangt
  • 5,343
  • 3
  • 23
  • 36
1

You can use a JPasswordField, which replaces characters by '*' by default.

You may want to read this:

Why does JPasswordField.getPassword() create a String with the password in it?

And if you're looking for an alternative to JPasswordField, you may want to read this:

Is there an alternative to JPasswordField?

Community
  • 1
  • 1
TacticalCoder
  • 6,275
  • 3
  • 31
  • 39
  • If I use JPasswordField,it makes me to create create a form.I don't want to do that,Is there a way uisng JOPtion pane?? – user10101 Mar 29 '12 at 11:27
  • @user10101 `JOptionPane` can display `Component`s, so you don't need to create a `JFrame` or a `JDialog` (I assume that's what you mean by "form") in order to use a password field. – lhballoti Mar 29 '12 at 14:13
  • exactly,JOptionPane can display Component but how I hide the data that he is entering in JOption pane??? – user10101 Mar 30 '12 at 06:32