Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Key Listener for a JComboBox

Posted on 2001-09-10
4
Medium Priority
?
910 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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.
Suggested Courses

610 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