Solved

Key Listener for a JComboBox

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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…

705 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