File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Editable JComboBox that must accept only alphabets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Editable JComboBox that must accept only alphabets" Watch "Editable JComboBox that must accept only alphabets" New topic
Author

Editable JComboBox that must accept only alphabets

sandy
Greenhorn

Joined: Jun 26, 2002
Posts: 11
Hi
How can a editable JComboBox made to accept only Lowercase and Uppercase Alphabets ? Any suggestions ?
Sandy
:roll:
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
Hi,
I see two possibilities. The easier one is to check, when actionPerformed event is fired, which element of the combobox is selected. Then you have a string and you can parse every single character, if it is a right one.
The second possibility I see, you need to register a KeyListener to the editor of the ComboBox. To get this, you need to call:
JComboBox.getEditor().getEditorComponent();
Then you catch every keyTyped event and you ask for a correct input.
Hope this helps
Rene
sandy
Greenhorn

Joined: Jun 26, 2002
Posts: 11
Hi Rene
Thanks a lot for the suggestion, but my query was if the user types in some string, how can the ComboBox prevent him from typing the wrong characters i.e the validation should be correct only for Alphabets, something of what a PlainDocument does for the TextField validation.Since ComboBox does'nt validate for Key events, how can this be achieved ?
Regards
Sandhya
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
Hi,
here is a little test:

Is this what you want?
sandy
Greenhorn

Joined: Jun 26, 2002
Posts: 11
Hi Rene
Thanks a lot ! It worked and yeah, this is what I wanted. Thanks again!!
Sandhya
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

"sandy" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

Thanks! and welcome to the JavaRanch!


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
wood burning stoves
 
subject: Editable JComboBox that must accept only alphabets