Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #2364 > unrolled thread
| Started by | kedward777@gmail.com |
|---|---|
| First post | 2012-12-18 11:20 -0800 |
| Last post | 2012-12-18 17:01 -0500 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.java.help
Need example how to use events with a jcombobox kedward777@gmail.com - 2012-12-18 11:20 -0800
Re: Need example how to use events with a jcombobox Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-18 15:17 -0500
Re: Need example how to use events with a jcombobox Gunter Herrmann <notformail0106@earthlink.net> - 2012-12-18 17:40 -0500
Re: Need example how to use events with a jcombobox markspace <-@.> - 2012-12-18 13:00 -0800
Re: Need example how to use events with a jcombobox kedward777@gmail.com - 2012-12-18 13:23 -0800
Re: Need example how to use events with a jcombobox markspace <-@.> - 2012-12-18 13:40 -0800
Re: Need example how to use events with a jcombobox Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-18 17:01 -0500
| From | kedward777@gmail.com |
|---|---|
| Date | 2012-12-18 11:20 -0800 |
| Subject | Need example how to use events with a jcombobox |
| Message-ID | <b87137f0-ad0d-4884-a6e5-0b2e534d34c9@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2012-12-18 15:17 -0500 |
| Message-ID | <kaqj03$7hh$1@dont-email.me> |
| In reply to | #2364 |
On 12/18/2012 2:20 PM, kedward777@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
esosman@comcast-dot-net.invalid
[toc] | [prev] | [next] | [standalone]
| From | Gunter Herrmann <notformail0106@earthlink.net> |
|---|---|
| Date | 2012-12-18 17:40 -0500 |
| Message-ID | <50d0f083$0$6574$9b4e6d93@newsspool3.arcor-online.net> |
| In reply to | #2365 |
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.
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2012-12-18 13:00 -0800 |
| Message-ID | <kaqlhn$olq$1@dont-email.me> |
| In reply to | #2364 |
On 12/18/2012 11:20 AM, kedward777@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).
[toc] | [prev] | [next] | [standalone]
| From | kedward777@gmail.com |
|---|---|
| Date | 2012-12-18 13:23 -0800 |
| Message-ID | <0cc9e12a-8bbb-4f61-929c-7017d72298a4@googlegroups.com> |
| In reply to | #2366 |
On Tuesday, December 18, 2012 4:00:37 PM UTC-5, markspace wrote: > On 12/18/2012 11:20 AM, 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). 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
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2012-12-18 13:40 -0800 |
| Message-ID | <kaqnsd$7te$1@dont-email.me> |
| In reply to | #2367 |
On 12/18/2012 1:23 PM, kedward777@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.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2012-12-18 17:01 -0500 |
| Message-ID | <kaqp4c$ga3$1@dont-email.me> |
| In reply to | #2368 |
On 12/18/2012 4:40 PM, markspace wrote:
> On 12/18/2012 1:23 PM, kedward777@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
esosman@comcast-dot-net.invalid
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.help
csiph-web