Link to home
Start Free TrialLog in
Avatar of dsignori
dsignori

asked on

setEchoChar

I'm using setEchoChar so that a password is displayed as asterisks.  I have a dialog used to change the password where I call setText on the TextField with the setEchoChar set.  The real password is always displayed however instead of the asterisks.  It looks as if the only time asterisks are displayed is when you are actually typing in the TextField but it doesn't work for setText.  How can I get this to work without doing setText("******").
Avatar of jpk041897
jpk041897

setEchoChar() activates a modifier for keyboard input, not for actual display. (I don't know why they designed it this way). You will have to simulate keyboard events to actualy get the behaviour your looking for.

If no one provides a better alternative,(or if you consider this one satisfactory)  I'll send you an answer with some code.
As no one has offere a better alternative, and if you are still interested I'll post you some code as answer.
I assume you are only using setText to setup the dialog so that
when a user brings up a dialog, the password field will be filled
with his default password.

The point to this is that since you don't want anyone to see the
password in the first place, you don't really have to send it
what it is. Instead, if his default password is 6 character,
send the entry box 6 asterisks.

For example, change your
  passwordBox.setText (password);
to
  String str = new String ();
  for (int i = 0; i < password.length (); i++)
    str = new String ("*" + str);
  passwordBox.setText (password);

Or better yet, send a predefined number of asterisks so potential
hackers won't know how many characters is in the default
password:
  passwordBox.setText ("********");

I missed your last sentence in your question:

  "How can I get this to work without doing setText("******")."

Anyway, I tried the following code on my Windows NT using JDK 1.0.2 and 1.1.2:

import java.applet.*;
import java.awt.*;

public class password extends Applet
{
  TextField pw = new TextField ("This is a test.");

  public void init ()
  {
    pw.setEchoCharacter ('*');
    add (pw);
    show ();
    pw.setText ("Neat!");
  }
}

Everything seems to work fine. The text is displayed as asterisks rather than the text. What OS platform and JDK implementation are you using?

Avatar of dsignori

ASKER

Comment for jpk.  Yes, I would like you to answer and post some code
if you will.  Thanks.
ASKER CERTIFIED SOLUTION
Avatar of jpk041897
jpk041897

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your question is verry close to beeing automaticaly graded. Why don't you F it sometime tommorow morning (tuesday,16) and send me an e-mail to jkelleghan@usa.net just in case.

I reaaly don't need the points. but you do need a solution so there is no sense in having the thread lost before I can get a definite answer to you.

And since the an answered question may be read by other users in the future, there is no sense in giving it a passing grade before we are sure that the solution posted works.

What do you think?