Solved

Creating a Java Masked Edit Textfield

Posted on 2003-10-31
5
320 Views
Last Modified: 2006-11-17
I'm trying to create a class that extends jTextField that closely mimicks the VB 6 MaskedEditControl.  The way I'm planning to implement it is: when the user types in the textbox, I want to check the new text against the mask before showing it.  It will filter out any text that does not match the mask.

The problem is, I don't know what event to use.  If I use the KeyPressed or KeyTyped events, everything works fine, and I can check the text against the mask, but whatever the user had typed in shows up at the end of the textbox.

If I use the jTextField's Document's textInserted event, I can check the new text against the mask, but, when I go to put the checked text back into the textbox, it of course fires the textInserted event, and I end up in an infinate loop.

So, my question is:  If I use the KeyPressed/KeyTyped event, can I somehow make it so that the Key that was Pressed/Typed is NOT inserted into the textbox?  -OR- If I use the Document's textInserted event, is there a way where I can stop listening for a textInserted event while I'm changing the text, then, start listening for the textInserted event again once I'm finished?
0
Comment
Question by:snapdrag0n420
  • 3
  • 2
5 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9659741
There is this:

It's a bit old, but should work...

http://www.sys-con.com/java/source.cfm?id=416
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 200 total points
ID: 9659764
This is the article that goes along with the code

http://www.sys-con.com/java/article.cfm?id=416
0
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 9661421
Try using a JFormattedTextField.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9662405
Ooooooh!  Good old Java 1.4!

See?  Told you that article was old :-)
0
 
LVL 92

Expert Comment

by:objects
ID: 10115118
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
print map entry 34 72
How to convert from xls to xlsx using java 7 55
How to determine if a string is a valid SHA value 7 35
Eclipse Java import and method not resolved 4 50
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question