• 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

How to clear a JTextField!!

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sam,

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


 
Samuel Woodhams
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic