Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to disable Japnese Character Input in JTextField  RSS feed

 
sachin pathak
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
clio katz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
clio katz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!