Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need example how to use events with a jcombobox

26 views
Skip to first unread message

kedwa...@gmail.com

unread,
Dec 18, 2012, 2:20:55 PM12/18/12
to
Hello,

I have a jcombobox that I initially populate it with just one "employee name".

Can you show me a code example how I would allow the user to "tap into" the jcombobox, and enter 3 characters of a name, and then have that fire an event which would fetch all matching employees and populate the jcombobox....

Thank you for any and ALL help!
Ken

Eric Sosman

unread,
Dec 18, 2012, 3:17:02 PM12/18/12
to
On 12/18/2012 2:20 PM, kedwa...@gmail.com wrote:
> Hello,
>
> I have a jcombobox that I initially populate it with just one "employee name".
>
> Can you show me a code example how I would allow the user to "tap into" the jcombobox, and enter 3 characters of a name, and then have that fire an event which would fetch all matching employees and populate the jcombobox....

I haven't done this myself and can't exhibit code, but I
can give you an outline that seems promising:

- You can use getEditor() on the JComboBox to obtain the editor
that displays and edits the selection.

- You can use getEditorComponent() on the editor to obtain the
component with which the editor works. If the editor is a
BasicComboBoxEditor (I think that's the default), this
component will be a JTextField.

- JTextField is a subclass of JTextComponent, so you can use
its getDocument() method to obtain the Document that actually
holds and modifies the text.

- You can use addDocumentListener() on the Document to be
informed whenever the Document content changes. Your
DocumentListener's methods will be called whenever the
user inserts or deletes a character.

- When your DocumentListener sees that an insertion has made
the text exactly three characters long, you can search your
employee list for the candidates that match, then use them
to update the ComboBoxModel.

- When your DocumentListener sees that a deletion has shortened
the text to two characters, you may want to empty out the
ComboBoxModel again. (User types JON, you stuff all the
JONESes and JONSONs into the model, he hits backspace and
types H, you'll re-populate with all the JOHNSONs instead.)

Please note that this is just an outline, with lots of detail
still to be filled in. (And please note, again, that I haven't
done this myself and can't promise it will work out.)

--
Eric Sosman
eso...@comcast-dot-net.invalid

markspace

unread,
Dec 18, 2012, 4:00:37 PM12/18/12
to
On 12/18/2012 11:20 AM, kedwa...@gmail.com wrote:

> Can you show me a code example how I would allow the user to "tap
> into" the jcombobox, and enter 3 characters of a name, and then have
> that fire an event

Eric's outline looks good to me. (I also haven't implemented such a
request.) In general the "editable combobox" example in Oracle's
tutorial will show you how to look up items in your combobox.

<http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html>

However the requirement for "three or more characters" is probably going
to require you to use the underlying document as Eric proposes. Swing
events usually aren't that fine grained, and you have to dig down to a
lower level component to get that kind of information out (and thus fire
an event when it happens).

kedwa...@gmail.com

unread,
Dec 18, 2012, 4:23:20 PM12/18/12
to

On Tuesday, December 18, 2012 4:00:37 PM UTC-5, markspace wrote:
Thank you Gentlemen,

I really didn't think it would be that difficult. Is there another approach that is well documented and more easily implemented? I can't believe this hasn't been done before(?)

Ken

markspace

unread,
Dec 18, 2012, 4:40:27 PM12/18/12
to
On 12/18/2012 1:23 PM, kedwa...@gmail.com wrote:
> Is there another
> approach that is well documented and more easily implemented? I can't
> believe this hasn't been done before(?)

Seriously, no. This is the best, and easiest, way I can think of.

Sometimes programming is just complex, period. Not much to be done
about that except suck it up and write the code.

The only gotcha in Eric's outline I might see is firing an event on
exactly 3 characters. I think you should fire an event on any character
change. The reason is you're probably going to want to search again for
4 character, 5 characters, etc. And if the user backspaces to 2
characters, you'll want to clear the search results. So firing an event
for every change sounds best to me.

Eric Sosman

unread,
Dec 18, 2012, 5:01:44 PM12/18/12
to
On 12/18/2012 4:40 PM, markspace wrote:
> On 12/18/2012 1:23 PM, kedwa...@gmail.com wrote:
> > Is there another
>> approach that is well documented and more easily implemented? I can't
>> believe this hasn't been done before(?)
>
> Seriously, no. This is the best, and easiest, way I can think of.
>
> Sometimes programming is just complex, period. Not much to be done
> about that except suck it up and write the code.

Right. The lower you go, the less help you get from the
layers you've bypassed, and hence the more you've got to do for
yourself. Kedward, consider yourself lucky to be handling
DocumentEvents instead of keyboard interrupts ...

> The only gotcha in Eric's outline I might see is firing an event on
> exactly 3 characters. I think you should fire an event on any character
> change. The reason is you're probably going to want to search again for
> 4 character, 5 characters, etc. And if the user backspaces to 2
> characters, you'll want to clear the search results. So firing an event
> for every change sounds best to me.

Sounds good to me. Might not want to populate the ComboBoxModel
when there are too few characters to keep the possibilities to a
reasonable limit (I imagine that's why he specified three). A more
flexible approach might take the number of matches into account
along with the prefix length, something like "Populate the model
when there are fewer than M matches, or when the prefix holds N
or more characters." That way, ZBIGNIEWSKI and ZORRO could pop
up as soon as Z was pressed, while STI might wait for more input
before trying to display STIBICH, STICH, STICHTER, STICKELS,
STICKMAN, ..., STIVER, STIVES, STIX, all umpty thousand of 'em.

--
Eric Sosman
eso...@comcast-dot-net.invalid

Gunter Herrmann

unread,
Dec 18, 2012, 5:40:29 PM12/18/12
to
Hi!

Eric Sosman wrote:
> - You can use getEditor() on the JComboBox to obtain the editor
> that displays and edits the selection.
>
> - You can use getEditorComponent() on the editor to obtain the
> component with which the editor works. If the editor is a
> BasicComboBoxEditor (I think that's the default), this
> component will be a JTextField.
>
> - JTextField is a subclass of JTextComponent, so you can use
> its getDocument() method to obtain the Document that actually
> holds and modifies the text.
>
> - You can use addDocumentListener() on the Document to be
> informed whenever the Document content changes. Your
> DocumentListener's methods will be called whenever the
> user inserts or deletes a character.

I have done something similar by changing the Document of a JTextField

private class SpecialDocument extends PlainDocument ....


textField.setDocument(new SpecialDocument())

This way I was able to create input fields that limit the input to
- a specific number of characters
- numbers only
- financial amounts (limited to a maximum number of digits left of the decimal point,
zero or one decimal points and a maximum of 2 digits after the decimal point).

In the cases above you ignore invalid entries and can notify the user if you want.

- AutoUpperCase all letters for certain input fields

HTH

Gunter in Orlando, Fl.

0 new messages