Article View: borland.public.jbuilder.java.swing
Article #9017Re: Typing in JComboBox doesn't cause the keyTyped event to be fired
From: Paul Furbacher [TeamB]
Date: Wed, 31 May 2000 00:00
Date: Wed, 31 May 2000 00:00
61 lines
2932 bytes
2932 bytes
Andreas Schnell wrote:
>
> Christer Brattlund <christer.brattlund@REMOVE_THIS.aerotechtelub.se> schrieb
> in im Newsbeitrag: 8gg5un$cs74@bornews.borland.com...
> > Ok, I found the answer in a message posted here earlier:
> >
> > ...
> > JTextField tf = (JTextField)cb.getEditor().getEditorComponent();
> > tf.addMouseListener(new java.awt.event.MouseAdapter() {...});
> > tf.addKeyListener(...);
>
> this works fine to get the input in a ComboBox, but it don't react on
> VK_TAB. For JTextFields is used
>
> field = new JTextField()
> {
> public boolean isManagingFocus()
> {
> return true;
> }
> };
>
> to manage the Focus myself, but what about JComboBoxes. I expect to do the
> same for the EditorComponent of a JComboBox, but how??
The reason we are talking about JTextField is that the default JComboBox
editor component is a JTextField. The statement
JTextField tf = (JTextField)cb.getEditor().getEditorComponent();
gets the editor component and typecasts it properly, assuming of course
that the default editor has not been replaced or its KeyMap has not
been altered (you would have to do that yourself, so unless you have
done so, the assumption is safe). If you drill-down into the code
of JTextField, you'll find all this info starting on line 72 of
JTextField.java. [I don't know how to stress this enough: drilling
down into class files is an essential activity: drill-down and
find, drill-down and find. Anyone reading this should go to bed
tonight and repeat "drill-down and find" until sleep arrives.
In this case, drill-down into JTextField and find "VK_".]
Therefore, handling events that occur in the JComboBox's editor
component is the same as it would be for a regular JTextField (because
it _is_ a regular JTextField).
Add an actionPerformed listener to the editor component to capture
VK_ENTER,
and add a focus listener (you want to handle focusLost) to the editor
component in order to catch VK_TAB. The simplest way to handle the VK_ENTER
would be to simply write
tf.transferFocus();
That fires a focus event. Put your actually handling in the focusLost
handler.
I think that covers just about everything.
Paul Furbacher (TeamB)
Technical Details
Message-ID:
<39358CC4.DA937076@home.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<8gg2u8$csb4@bornews.borland.com> <8gg5un$cs74@bornews.borland.com> <3934dd8a@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,d1eaa09f97497005 X-Google-Attributes: gid111d45,public From: "Paul Furbacher [TeamB]" <pnf@home.com> Subject: Re: Typing in JComboBox doesn't cause the keyTyped event to be fired Date: 2000/05/31 Message-ID: <39358CC4.DA937076@home.com> X-Deja-AN: 629636976 Content-Transfer-Encoding: 7bit References: <8gg2u8$csb4@bornews.borland.com> <8gg5un$cs74@bornews.borland.com> <3934dd8a@dnews> X-Accept-Language: en,pdf Content-Type: text/plain; charset=us-ascii Organization: Reverse Alchemy, Inc. Mime-Version: 1.0 Reply-To: pnf@home.com Newsgroups: borland.public.jbuilder.java.swing