?
Solved

Key Listener for a JComboBox

Posted on 2001-09-10
4
Medium Priority
?
907 Views
Last Modified: 2012-06-27
All,

I have a editable JComboBox, which the end-user can select an item from a list or enter an item in manually.  The problem is that I need to do checks on the characters that the user enters and only allow specfic character to be entered.  Therefore I need to implement so sort of Key Listener to the JComboBox.
I have tried adding a KeyListener and KeyAdaptor to the JComboBox, but it doesn't seem to be capturing the key events.
Any suggestions???
I am using JBuilder3 and JDK 1.3.1 by the way.

Thanks in advance!
0
Comment
Question by:Cuong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 2

Expert Comment

by:mzimmer74
ID: 6472398
A JComboBox is actually a component made up of several other components, one of them being a JList.  I suspect that what you may need to do is add the KeyListener to the JList rather than the JComboBox.  You can get the JList by getting the component list from the JComboBox by calling box.getComponents() which returns Component[], and then searching through that array for a JList Component.  Hope that helps.
0
 
LVL 9

Accepted Solution

by:
Ovi earned 400 total points
ID: 6473272
The problem that you are having is that when your combobox become editable, you must retrieve the EDITOR associated to this operation and listen for key events inside of it. Usually the editor is represented by a simple JTextField.

something like :

myCombo.getEditor().addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent ae) {
    **************************
  }
});

or
myCombo.getEditor().getEditorComponent() and mess around with this one.
0
 

Author Comment

by:Cuong
ID: 6473784
Excellent! Thanks alot!
0
 

Author Comment

by:Cuong
ID: 6496626
Just what I want
Thanks alot!!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question