Skip to Content
Swing Hacks
book

Swing Hacks

by Joshua Marinacci, Chris Adamson
June 2005
Intermediate to advanced
544 pages
22h 24m
English
O'Reilly Media, Inc.
Content preview from Swing Hacks
Auto-Completing Text Fields #50
Chapter 7, Text
|
265
HACK
When you type a comma, a question mark, or lowercase letters, they are
ignored.
You can do smart things with this tool, but also really stupid things. Let’s
say you want to restrict input to the characters that will be in a North Amer-
ican phone number of the form 123-456-7890. You can set the pattern to
[0-9\-]*, which will allow the user to type in only numbers and the hyphen.
Now you’re feeling clever, but you don’t like the fact that this still allows
users to type patterns that aren’t phone numbers, like
1111111 or -1-1-1-1.
So, you set the pattern to an exact description of the phone number pattern:
[0-9]{3}-[0-9]{3}-[0-9]{4}
But now your users can’t enter anything! Why? Because although that regu-
lar expression does specify the phone number pattern, no substring of it will
ever match. This expression specifies exactly 12 characters, so when you
type the first one, there’s only one character—it doesn’t match the pattern,
so it’s rejected.
The moral of the story here is to be thoughtful. You might decide to wire up
a
FocusListener so you impose the regex pattern only when the user moves
off the field, and change the “delete everything” behavior to something a lit-
tle less forceful; for example, you could pop up a dialog telling the user that
her input isn’t in the right format. Just don’t be surprised when regular
expressions give you results ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Swing GUI Programming

Learning Swing GUI Programming

Brian Cole
Java Swing

Java Swing

Robert Eckstein, Marc Loy, Dave Wood
JFC Swing Tutorial, The: A Guide to Constructing GUIs, Second Edition

JFC Swing Tutorial, The: A Guide to Constructing GUIs, Second Edition

Kathy Walrath, Mary Campione, Alison Huml, Sharon Zakhour

Publisher Resources

ISBN: 0596009070Purchase bookErrata Page