Win a copy of AWS Security this week in the Cloud/Virtualization 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

validate JTextField - different scenarios?

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working my way through the scjd book (AM & TC).

I want to validate a JTextField as 8 digits. The PositiveInteger class (using extends PlainDocument) example does nearly what I want and I have tweaked a version to stop checking when 8 digits entered.

However, if you back up and remove a few digits and then start adding more in the middle. The starting offset is not what I was hoping it would be, because of this it goes past 8 digits. It appears to be giving the offset starting from the point of new digits being entered and not the start of the field. Is there a work around for this?

public void insertString(int offs, String str, AttributeSet a)

From looking ar previous posts there are many ways to do this but the one I will use will be one of the following 3:

1 MaskFormatter will functionally provide what I am looking for and is the easiest to implement. But it's cheap and cheerful.
2 extends PlainDocument : I like the feel of the way the PositiveInteger works. Should I keep tweaking until I nail down the offset or will it not do what I am looking for?
3 DocumentListener: have'nt fully explored yet.


Regards, Robert

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Robert,

I also doubted between all the alternatives to block invalid input, started a thread in the swing-forum here. So used the DocumentFilter.
For the assignment I created 2 filters: one allowing a maximum number of chars, the other one allowing a maximum number of digits. The last one I used for the customer id.

Kind regards,
Roel
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

my approach was to use a JOptionPane together with the String.matches() method.

Works perfect and is a real simple solution
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic