🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #9287

Re: Detecting new items in the JComboBox

#9287
From: Paul Furbacher [TeamB]
Date: Thu, 20 Apr 2000 00:00
81 lines
3982 bytes
James Cook wrote:
> 
> I haven't had the opportunity to work with an editable combobox...until now.
> There doesn't seem to be a special event for this activity. I see two
> possibilities:
> 
> 1. Since a combobox is a composite component, I could add a document
> listener to the underlying JTextField. Even so, I'm not sure if this would
> gain me anything.
> 
> 2. Listen for actionPerformed events. If the current selectedItem() is not
> in the model, add it.

This is what I did, but I'm not going to claim that this
is the most elegant approach.

The two problems you encounter are how to trap loss of focus and 
how to handle VK_ENTER.  Oddly enough, the latter doesn't trigger
a loss of focus -- it causes an actionPerformed event.  

As you mention, the JComboBox is a compound component, and 
instead of the component offering you convenience methods
to add listeners to the internal text component, you have
to dig into the combobox, get the editor, get its "editorComponent"
and then begin adding your listeners.  That makes for some 
pretty long lines, what with all the typecasts and all.

  JTextField tf = 
      (JTextField)myCombobox.getEditor().getEditorComponent();

Since the VK_TAB event is fired off as a focusLost event, the
simplest approach would be to have the VK_ENTER key event cause
a focus transfer:

  tf.addActionPerformed(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent ae) {
      ((Component)ae.getSource().transferFocus();
    }
  });

Finally, you add a focus listener to the textfield and handle
that by setting the text, if different, into the combobox's
list model.

  tf.addFocusListener(new java.awt.event.FocusAdapter() {
    public void focusLost(FocusEvent fe) {
      setItemIntoComboboxList(e.getComponent());
    }
  });

The "setItemIntoComboboxList()" accepts a component, presumably
it will be the combobox in question, and just checks to see
whether the text is already in the list.  If not, it just
appends it to the bottom.  The method can be re-used for 
any number of comboboxes in the same panel/frame -- whatever
the encapsulating entity is.

  private void setItemIntoComboboxList(Component c) {
    String s = (JTextField)c).getText(); // can be a dicey typecast
    // get the combobox which "owns" this textfield
    JComboBox cb = (JComboBox)((Component)c).getParent(); 
    // Get the model, making the assumption that it a default model...
    DefaultComboBoxModel model = (DefaultComboBoxModel)cb.getModel();
    if ( model.getIndexOf(s) == -1 ) // i.e., not found
      cb.addElement(s);
  }

I have to admit that I fumbled around for a while trying to 
figure out just what to trap and where.  Swing just doesn't 
make the simplest things easy.  You have to wonder why we've 
taken such a big step backwards on so many fronts in UI programming 
with Swing.  It feels as clunky as TurboVision for DOS where
just about everything had to be customized just to work "normally".
That was more than 10 years ago, and we're right back at it.
The cumulative hours of searching the Swing code base isn't
really very productive, nor is reading through 1000-page tomes
on Swing just to do the ordinary.  There has to be something
better than this, more productive right out of the box.


Paul Furbacher (TeamB)
Technical Details
Message-ID: <38FF2340.B82263CF@home.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <8dkb48$98b4@bornews.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,34aa599bebad14c9
X-Google-Attributes: gid111d45,public
From: "Paul Furbacher [TeamB]" <pnf@home.com>
Subject: Re: Detecting new items in the JComboBox
Date: 2000/04/20
Message-ID: <38FF2340.B82263CF@home.com>
X-Deja-AN: 613619258
Content-Transfer-Encoding: 7bit
References: <8dkb48$98b4@bornews.borland.com>
X-Accept-Language: en,pdf
Content-Type: text/plain; charset=us-ascii
Organization: Another Netscape Collabra Server User
Mime-Version: 1.0
Reply-To: pnf@home.com
Newsgroups: borland.public.jbuilder.java.swing