Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Why JFormattedTextField Behaviour changes so much?

 
Ranch Hand
Posts: 93
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

taking as example the code from:
https://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#ListDemo,

being like this:





When changing:



by:




behaviour changes so much...

This is:

1) Hire button must be pressed twice instead of one to add a new elemet to JList.
2) If press Hire once the JFormattedTextField is emptied, it shows an element.


I would like to have the advantages of original version but having the Formatter as well.

What changes should I do?

Thanks in advance!!

 
Rancher
Posts: 3111
26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I"ve never been a big fan of the JFormattedTextField. It has some quirks.

I would guess that what is happening is that the order of events is not what you expect.

That is the data you type into a formatted text fields needs to be edited to verify that the text you entered matches the mask.

If the data is valid that the changes are "commited".

This validation is done when the text field loses focus.

So it would appear that the ActionListener code is executing before the focusLost code of the formatted text field.

Another approach is to use a DocumentFilter. This allows you to edit the data as is is being typed.

For example:


Now this will function like a normal text field except the data is converted as it is typed.
 
Alfonso Sanz
Ranch Hand
Posts: 93
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob for your prompt and expert answer. Indeed seemed to be linked to focus loss.
 
money grubbing section goes here:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!