Search code examples
java-metextfieldmidpcaretlcdui

How to set caret position in textField in j2me?


Is it possible to set caret position in TextField in J2ME? There's a method getCaretPosition(), but I need to set it.


Solution

  • In MIDP 2, TextField API does not provide a way for application developer to set caret position.

    The most likely reason is that API designers decided that exposing such an API could led to negative user experience: imagine application suddenly moving caret from under your fingers right when you're typing a text - that's how setCaretPosition would work. If you plan something like that in your application, think twice about how it would look for its users (I for one would probably broke my phone and curse a MIDlet that would behave like that).

    • side note - purpose of "auxiliary" method getCaretPosition you mentioned is to support insert API:

      ...If the application needs to simulate typing of characters it can determine the location of the current insertion point ("caret") using the with getCaretPosition() method. For example, text.insert(s, text.getCaretPosition()) inserts the string s at the current caret position...