Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2365

Re: Need example how to use events with a jcombobox

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.help
Subject Re: Need example how to use events with a jcombobox
Date 2012-12-18 15:17 -0500
Organization A noiseless patient Spider
Message-ID <kaqj03$7hh$1@dont-email.me> (permalink)
References <b87137f0-ad0d-4884-a6e5-0b2e534d34c9@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web