Solved

Key Listener for a JComboBox

Posted on 2001-09-10
4
896 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
  • 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 100 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now