Take the 2-minute tour ×

I'm trying to write an application which will search and retrieve a user's profile using data using an id key found using a scanned barcode or embedded in a magnetic strip card's data. The latter one is cause me grief. The magstripe data needs to be parsed prior to searching for the user profile.

My question, is there a way to capture the text scanned into the textbox and parse it before it gets displayed in the textbox?

My reader/scanner is the keyboard emulation type so its as if each character encoded on the stripe is typed out in the textbox. I guess a solution (but is it the best?) would be to intercept each keystroke (emulated by the magnetic stripe reader), store them in a buffer and display an empty character until the end of the read string. Once the end of card's data is read, I could parse and display the id part of id. Problem is... how do you know it's the end of the card's data string if they get inputted as individual char keystroke?

share|improve this question
Please provide a link to the device's API and an sscce showing what you've tried. –  trashgod Sep 27 '11 at 2:48
What is a textbox? I've don't know of any Swing component by that name? Be specific and use the proper class name so we don't have to guess what you are talking about. –  camickr Sep 27 '11 at 3:27
@trashgod There's no API for the MSR as it's a keyboard emulator. While thinking some more about the problem, after posting, I decided to put a button with an ActionListener which parses the data read in by the MSR. This works but has the disadvantage of showing the raw data. –  Eric A. Gravel Sep 27 '11 at 17:03
I think I can work around that issue by doing what Rajeev and Camickr were suggesting, use DocumentFilter to listen for the start sentinel character, start buffered the character and stop when you encounter the end sentinel at which point I'll parse the data. For each buffered character, I'll replace the field's text to empty string and only set it with the parsed id once the end sentinel is reached. this should work. –  Eric A. Gravel Sep 27 '11 at 17:04
I see. @Rajeev was able to suggest an approach even without knowing the concrete API, but a link might help future visitors. Don't forget to up-vote as your reputation grows. –  trashgod Sep 27 '11 at 17:17

2 Answers 2

up vote 1 down vote accepted

Answer to your first question is to set a DocumentFilter on the Document of the textfield.


Your MSR would surely emit a START_STRING and an END_OF_LINE_STRING as a combination of some predefined characters. Read the data specification of the MSR device. Once you have that, you could implement the insertString of the filter similar to this pseudo code

    then clear buffer

    then parse and do super.insertString

    append string to buffer

Again, the parse logic can be implemented using the data specification of the MSR.

(MSR = Magnetic Stripe Reader)

share|improve this answer
Rajeev, that's similar to what I thought after thinking about this some more. Magnetic stripe data (if encoded similarly to credit card) have a start and end sentinel character. Just to test it out I put a button and attached an ActionListener to parse out the text. This works but I just don't like seeing the raw data in the input field. I know I can replace it once the button listener fires but I'd prefer not even showing the raw data. I'm trying to see if there's a way to intercept it before it's being displayed, parse it and insert in the field only the id part of the raw data. –  Eric A. Gravel Sep 27 '11 at 16:48
I guess I can do like you and camickr suggested and have a DocumentFilter that will look for the start sentinel. Once found, all subsequent characters, along with sentinel, could be put in a buffer until the end sentinel is found. For each character entered, the filter would replace the field's text with empty strings. Once end sentinel is reach it would parse the data into a domain object and display in the field the id retrieved from the magstripe. I'll give it a try tonight. –  Eric A. Gravel Sep 27 '11 at 16:57
+1 for abstraction. –  trashgod Sep 27 '11 at 17:14

You should be able to use a Document Filter to intercept the text as it is added to the Document of the JTextField. Then when you receive the end of string character you can parse the text and insert it into the text field.

share|improve this answer
Thanks camickr, that's similar to what Rajeev was suggesting and which I was starting to think would be the solution. I'll just have to listen for the start and end sentinel characters and buffer the data in between the two. –  Eric A. Gravel Sep 27 '11 at 16:58
@Eric, acutally I suggested it an hour before Rajeev. Your question was about capturing the text before it was added to the text field which I answered. I left the parsing up to you since I don't know the format of your data. –  camickr Sep 27 '11 at 17:00
Ah! I just noticed that the site shows them chronologically descending. I just assumed the first answer that I encountered was the first one submitted. My apologies. And you're right, my question was about how not to show the raw data but rather the particular piece of data within it. As for data format, there are many since I'd like to locate record using various forms: by name, by barcode, membership card with mag stripe, or DMV license. Thanks again. –  Eric A. Gravel Sep 27 '11 at 17:13
+1 for precedence and reference. –  trashgod Sep 27 '11 at 17:13

Your Answer


