aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Displaying number of Characters entered in JTextfield Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Displaying number of Characters entered in JTextfield" Watch "Displaying number of Characters entered in JTextfield" New topic
Author

Displaying number of Characters entered in JTextfield

Brendan Thompson
Greenhorn

Joined: Apr 27, 2012
Posts: 21
Hi I am almost done my intro to Java course assignments but am having a hard time trying to figure out the following question:

Create a password entry application that accepts the password but only shows the number of characters entered so far. Then, add a button that allows the user to toggle between seeing the password and seeing the number of characters in the same password field.

As of right now I am using a JTextField instead of JPasswordField because I'm not quite sure of how to display contents of a password field. The problem I am having right now is trying to display the number of characters that have been entered into the textbox. When I run the program, the number of characters typed only goes upto 2. Here is my code:



Here is my test class:



I know the problem is that every time a key is typed, the String is redefined as the current text in the box, this is why I can't get anymore than 2 characters to show up. I just don't know how to resolve the problem. Thanks for reading this.

Bill Johnston
Ranch Hand

Joined: Nov 17, 2005
Posts: 201
I don't see that in this case you need a MouseListener. Also, to get rid of all those empty methods your KeyHandler can extend KeyAdapter instead of implementing KeyListener. But that is really off the subject, just a couple ideas to kick around.

As to your problem, what I think you might do is, in your KeyHandler, add keyPressed code, setting the password field to "". Then in your keyReleased method make the initial statement

Also for your numOfCharactersSting, you can just assign to it


~Bill
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Bill Johnston wrote:I don't see that in this case you need a MouseListener.

Nor a KeyListener.

The way to go is to use a DocumentFilter. You will need to cache the entered characters separately, maybe in another Document that isn't associated with the text field, or in a List<Character> or String.

You should be able to see why your present code never shows more than two characters typed.

What String do you think that returns when you have already changed the text? Certainly not


luck, db
There are no new questions, but there may be new answers.
Brendan Thompson
Greenhorn

Joined: Apr 27, 2012
Posts: 21
The mouse listener wasn't needed I understand, was only using it for cosmetic use to clear the prompt when clicked. I will try what you guys have said to try and get this running. Thanks for the useful responses.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Brendan Thompson wrote:The mouse listener wasn't needed I understand, was only using it for cosmetic use to clear the prompt when clicked.

Did you realize that gives you differing behavior if the user presses <Tab> to move to the field?

That 'cosmetic use' is a case for a FocusListener.
Bill Johnston
Ranch Hand

Joined: Nov 17, 2005
Posts: 201
Darryl Burke wrote:
Bill Johnston wrote:I don't see that in this case you need a MouseListener.

Nor a KeyListener.


I generally try to answer questions in the closest context in which I perceive they're asked; closest to the code already shown. That means the answers are often not the best solutions but are the closest to what the poster already has, at the same time assuming that some other/s will flesh out a fuller explanation and better overall solution - which almost always occurs. My thinking is that either the requester wants the simplest solution now or that, if a student, has not gone much further in his or her studies yet. I fully appreciate other points of view - which I suppose is why so many types of answers are usually given to problems.
Brendan Thompson
Greenhorn

Joined: Apr 27, 2012
Posts: 21
I am pretty new to Java, only been using it for about a month. I wasn't able to come up with a solution for this problem yet. I gave up for now and asked my tutor if the question wants to display the number of characters in a different label instead of directly in the text/pass field.

Thanks for the help, unfortunately I couldn't figure out how to use documentfilters so I never tried that.
Bill Johnston
Ranch Hand

Joined: Nov 17, 2005
Posts: 201
Brendan Thompson wrote:I am pretty new to Java, only been using it for about a month. I wasn't able to come up with a solution for this problem yet. I gave up for now and asked my tutor if the question wants to display the number of characters in a different label instead of directly in the text/pass field.

Thanks for the help, unfortunately I couldn't figure out how to use documentfilters so I never tried that.


Don't give it up. In my first post I gave you an answer that only requires a very few changes. I don't argue with Darryl about it being an optimum choice, but it will get you there for now. Re-read what I posted and ask back if you don't understand my post.
Brendan Thompson
Greenhorn

Joined: Apr 27, 2012
Posts: 21
Thanks so much for your help Bill. I was just notified that I was really only supposed to display the whole password using echo char. Then I was supposed to implement a toggle switching between the echo char password and the unhidden password. Felt silly when I realized my mistake.
 
jQuery in Action, 2nd edition
 
subject: Displaying number of Characters entered in JTextfield