aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to clear a JTextField!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to clear a JTextField!!" Watch "How to clear a JTextField!!" New topic
Author

How to clear a JTextField!!

Samuel Woodhams
Ranch Hand

Joined: Sep 01, 2009
Posts: 31
Hey,

i have a problem! i have a piece of code that runs when a user uses a JButton, this code is therefore in an actionlistener. the problem is i want to clear the textfield everytime the button is pressed in preparation for a new string input. The problem is that it does clear! im pretty sure it has to do with the way im clearring it but i don't know why, so if anyone could help it would be most appesiated.

Here's the code


thanks in advance

sam
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
Hi Sam,

You give a new value to the text field immediatelly after the clear:


Samuel Woodhams
Ranch Hand

Joined: Sep 01, 2009
Posts: 31
yer i no i do, but thats the point!!! i do that coz if i don't for some reason when i next run the script it just adds the String onto the all ready excisting String that is in the Textfield!! but the point is im not sure why it does that, coz i thought that when i use the textfield.setText() method it completely rewrites the textfield. but for some reason its not and i don't know why ??

thanks for the reply though!

sam
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
Unfortunaelly not every aspect of your problem is clear to me. Maybe if you post a runnable code we can help easier.
JTextField's setText sets the text not appends it, so the first setText clears the text(just try it without setting the password) and the next one sets the password. I think your problem comes from the

line, since you not clear the password before appending the new one.
Samuel Woodhams
Ranch Hand

Joined: Sep 01, 2009
Posts: 31
right sorry!! its a long code so be prepeard to get annoyed with it (also i want to say sorry coz i think i was a bit rude in the last post)!

this is it in full coz i really don't know where the error could be coming from so i don't want to leave anything out.

this project contains;
main.java
genarate.java
charselect.java
Copy.java
inputlimiter.java

main.java

genarate.java

charselect.java

Copy.java

inputlimiter.java


thats it!

as i said i think the problem is in the passwordgen method in the genarate class but im really not sure!

thanks in advance

sam
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
I don't think you were rude, so don't worry. I tried the code and as I said your problem has nothing to do with JTextField but with the password StringBuffer. You use a global instance for the 1st, 2nd, 3rd, ... passeord generation so you get a result which contains the 1st, 1st + 2nd, 1st + 2nd +3rd password as a string. Just think about it a little bit if it is not clear.
I also advice to read some of these coding conventions which makes your program easily understandable:
Sun
Javaranch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38896
    
  23
Don't use == to compare reference types for equality. Don't say == true (or == false). Those uses of == are prone to errors, the first because they are liable to return false when you want true, the second in case you write = by mistake.

You may be able to reduce that enormous block of if-elses to
Main.alpha = charset.equals("Alphanumeric etc etc");
Main.numeric = charset.equals("Numbers etc etc"); etc

You should use get and set methods rather than public attributes for those booleans.

For reasons explained here, avoid abbreviations like "coz".
Samuel Woodhams
Ranch Hand

Joined: Sep 01, 2009
Posts: 31
Miklos

after starring at my code for half an hour i finally got what you were telling me! so i change the code to this so it now works


so thanks for that.

Campbell i know it makes the code not very concise, and i would like another way of writing it, but i don't understand how to use your code to do it! could you explain it in more detail/give me a example to run, so i can work out what to do!

thanks again

sam
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38896
    
  23
That looks a lot better And about 95% shorter than before.
The only improvement I would suggest is you could explore the methods of the Random class which return ints.
 
wood burning stoves
 
subject: How to clear a JTextField!!