Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Password Generator doesn't clear JTextField  RSS feed

 
Dylan Black
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.

 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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".
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!