• 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

Password Generator doesn't clear JTextField

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I was bored this evening, and I decided to make a random password generator, where the user specifies the length of their password and then I generate a random password of that length. The generating part doesn't do anything wrong, it's the last part, where I clear to JTextField at the end so it doesn't make a super long password. I am using setText(""); on my JTextField and it still won't work. Help! I've tried doing this in different sections of my code and it still won't work!  Code is below.

 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're looking in the wrong place. The problem is not that JTextField.setText() appends data to its content -- it doesn't. You could remove line 63 from that code without changing how it works at all.

So if you write the contents of the "password" variable into a JTextField repeatedly and the contents of the JTextField get longer and longer, the problem isn't with how you are writing the contents of "password". The problem is with the contents of "password".
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I can also see some problems with your GUI. You shou‍ld probably not make those variables fields of the class; they will probably work if you convert them to local variables. Don't create a new Random object every time you run a method. See if you can reuse the same instance. Don't use null layout nor “absolute” positioning, despite what you see in some books. Miss out the getContentPane calls, because they are probably not necessary. Don't make a display class implement a Listener. Create your own Listener class. Move your setVisible call to the last line. Don't use escapes for the line end characters. You probably don't need a line end for a tooltip anyway.
 
reply
    Bookmark Topic Watch Topic
  • New Topic