Solved

Creating a Java Masked Edit Textfield

Posted on 2003-10-31
5
319 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 49
windows explorer path to command prompt 5 44
Tomcat: Unable to run tomcat service. 2 18
Problem to Alipay 10 22
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

776 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