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

jTextField not being cleared  RSS feed

 
peter m hayward
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in my application i have several TextFields to allow book data to be entred at the end of the collection cycle the data contain within them is sent to mysql after which all the TextField have to be cleared ready for the next book, this happens by calling my clear method which works fine but misses out one of them which does not get cleared it does however if the method is called for the second time here is the code

i have check the properties of   jTextField14 and they are no different to those that get cleared i have trie moving  jTextField14 to the top of the list to no avail 
 
Junilu Lacar
Sheriff
Posts: 10878
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then the problem is not in the code you posted but elsewhere. I would search for all references to that particular field and see where its text is getting set. I'd trace what happens between the first call to clear() and the second call to clear().
 
salvin francis
Bartender
Posts: 1479
21
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter,
While you are searching for an answer, I would like to point out that jTextField1, jTextField2, etc. are not good variable names. You have written comments to understand them as "//Author"
Why cant you use authorTxt (or similar) instead of jTextField1 ? It would be less confusing.

 
peter m hayward
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Hi Peter,
While you are searching for an answer, I would like to point out that jTextField1, jTextField2, etc. are not good variable names. You have written comments to understand them as "//Author"
Why cant you use authorTxt (or similar) instead of jTextField1 ? It would be less confusing.


these are the names allocated by netbeans maybe i could change the name of them which is something else i need to look into at some point as i totally agree with you about the names but for now i just want to find the reason one of them is behaving oddly
 
Fred Kleinschmidt
Bartender
Posts: 507
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does your clear() method call setName("") on all of the components? This seems like a really dumb thing to do since all components now have the same null name.

Assuming you might have changed the background and when cleared need to reset the color, you could cut the code in half by creating functions like clearText(), clearLabel(), etc. For example:

then your clear() method just calls this method for each test field that needs to be cleared. You could also place all of the text fields in an array or ArrayList and use something like:

or even use the Java8 streaming feature to reduce that to one line.

Note also that you might have problems with component sizes when you alter the text, depending on the layout manager being used.
 
peter m hayward
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:Why does your clear() method call setName("") on all of the components? This seems like a really dumb thing to do since all components now have the same null name.

Assuming you might have changed the background and when cleared need to reset the color, you could cut the code in half by creating functions like clearText(), clearLabel(), etc. For example:

then your clear() method just calls this method for each test field that needs to be cleared. You could also place all of the text fields in an array or ArrayList and use something like:

or even use the Java8 streaming feature to reduce that to one line.

Note also that you might have problems with component sizes when you alter the text, depending on the layout manager being used.

I test the name of each to ensure that the data has been entered e.g once the data has been enter into the author text field its name is change to authorOK so the validation method checks this and only allows the process to continue if all the names have been changed  i shall consider your suggestion  to reduce the code but this still does not answer why one of the text filed is behaving oddly
 
Campbell Ritchie
Sheriff
Posts: 53742
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
None of your code shows any of the text components having the text cleared.
 
peter m hayward
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:None of your code shows any of the text components having the text cleared.


not too sure what you mean here is the first one jTextField1.setText("");//Author which clear the text!
and the rest are the same and it works till it get to jTextField14.setText("");//fettle     which only work after calling clear a second time
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:Why does your clear() method call setName("") on all of the components? This seems like a really dumb thing to do since all components now have the same null name.


Except that the "name" of a component is almost useless, and there's nothing in the Swing code (as far as I know) which does anything with it. User code can get the name and do something with it, if that's a useful thing to do, but I can't see any use for it myself.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If jTextField14.setText(""); fails to clear a component which is visible in the GUI, then that means it's clearing some component which isn't visible. It's quite possible that component was never added to the GUI. That could happen if you have two different lumps of code which declare components, one declaring them as instance variables and the other as local variables, and the one assigned to a local variable is added to the GUI and the one assigned to the instance variable isn't. Or something like that.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My suggestion:
Stop using all these text fields. Modify your code to use only one. Once it's working properly as expected, add the rest of the fields.
 
peter m hayward
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all for trying to help with this issue This morning  i finally solved it and to be honest i still have no idea as to why this was the case anyway here is how i solved it deleted the textfield and replaced it from the components  pallet put the code back in the action performed and it now works perfectly
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!