Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex constraint on a textfield

 
Jess Dancel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, all.

I want to put a constraint on a JFormattedTextField that will only allow characters matching a regex to be entered (in this case, only hex digits 0-E).

The possible solutions I've found to this are all quite complicated. I know I've done it before and it was simple, but having trouble remembering.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jess,
Welcome to JavaRanch!

This character class matches hex: [0-9A-E]

What else are you looking for that was complicated?
 
Jessica Dancel
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering how to add that as a constraint to a textfield, so that the user can only enter those characters.
 
Hiroshi Iwatani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use MaskFormatter. See the doc of the class for its usage.
 
Jess Dancel
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Hiroshi! MaskFormatter did exactly what I needed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic