• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Displaying number of Characters entered in JTextfield

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Brendan Thompson
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic