Forums Register Login

How to disable Japnese Character Input in JTextField

+Pie Number of slices to send: Send
Hi

I need to allow only English character input in JTextField. Can anybody help. The complexity is increased because the Text can be pasted also in the textField. It is ok if anybody knows how to disable the paste action for the JTextField.

Thanks in advance

Sachin Pathak
+Pie Number of slices to send: Send
Hi Sachin,

I don't know if this will help, but ...

what to filter:

perhaps you could try filtering the text input based upon the character code (codepage) ... the japanese character sets should map-out to - i think - double-byte characters in a different range than english codepage. you can pick this up by the unicode chars in a given input string. s.b. documented in unicode dox


my guess is that this filter might be too weak, because you might still be exposed to other action like text-delete... which may be undesireable too.

A stronger filter could also be based upon user's language/country, as in

String user_lang = System.getProperty("user.language");
String user_country = System.getProperty("user.country");

this info could be gathered at application init time.


how to filter:

With the "strong" filter option, you can turn editing on/off when you instantiate the JTextField, based upon user lang/country.
Done!

(It's probably worth noting that the 'strong' filter is not bullet-proof, but if the scenario fits - it should provide a decent deterrent)


With char-based filtering, you could implement a DocumentListener (KeyListener might be more effective). Optimally you can sample the input initially, then go into a session-long edit/no-edit mode. [it's kind of a drag to listen to keypresses for long...slows things too]


where 'this' is a class that implements DocumentListener (_changedUpdate, _insertUpdate, _removeUpdate)

Your change and insert listeners can sample/filter the char codes of the document string, and cancel the input ... and even shut-off editing to the component if the charset is wrong.


Since you probably have more than one JTextField, the easiest thing to do is probably write a JTextField extension. Sun has a good example in the 1.4.2 API doc.


shutting off paste (import):

you could try

jtMyTextField.setTransferHandler(null);


(could otherwise install your own TransferHandler with this method, if you want control over import of text)


hth
+Pie Number of slices to send: Send
...just happened across a Java construct that may be more useful for filtering "meaningful" text ... see:

Setting Expected Character Set

in the Java Input Client API Tutorial.

hth
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2300 times.
Similar Threads
Help on JTextField
how to avoid user to enter a numeric or alfanumeric input to JTextField
How to disable selection in a JTextField?
Please Help for JSF inputCalendar component
i18n Basic
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:00:33.