Solved

Key Listener for a JComboBox

Posted on 2001-09-10
4
898 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

803 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