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
270
|
Chapter 7, Text
#50 Auto-Completing Text Fields
HACK
You can hide the window at this point and look at the selection value. Since
the
Completer just puts Strings in the list model, you can pull out the
selected value and set that as the text of the field. And you’re done.
Well, not quite. If you call
setText( )
directly in
valueChanged( )
, you’ll be
thrown an
IllegalStateException
. The problem is that you’re attempting to
change the value of the
Document
while it’s already being changed. In other
words, firing off the
DocumentEvent is part of the document edit that began
with the user’s keystroke that led to the completion menu appearing; this
edit needs to complete before another is attempted. As you can see in this
hack, the workaround is to create a worker thread to set the field with the
clicked text, and to invoke that worker on a later cycle through the event
loop. This arrangement returns immediately and lets the first edit finish,
then sets the field later.
Test Out Auto-Complete
To test this class, you need to put the CompletableJTextField in a GUI and
provide a way to give it some completions. The
TestCompletableJTextField
class in Example 7-5 does just that, offering a second JTextField where you
can enter strings that will be offered as completions to text typed into the
CompletableJTextField. When run, the test GUI originally looks like
Figure 7-3.
Example 7-5. A GUI to exercise the CompletableJTextField ...
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