We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi there,
I know this sounds extremely simple, but I have been looking around a lot and have not been able to find a solution for it. The closest thing I could find was the getIndex() method for the Textfield class which I think tells me where the cursor is, but I cannot set the index (i.e. tell the cursor where to be and I do not know why). Has anyone got any ideas how to do this?
Cheers!!
Answers
Explore my posts on April 3rd and 4th in https://forum.processing.org/two/discussion/comment/94617#Comment_94617
Kf
@kfrajer Interesting. But the thread is inconclusive, correct ? Are you still trying to implement the arrow keys in the Textfield?
One of the links did a good job from what I saw. Give it a try and if possible, provide feedback of what is missing or if it works to your end.
Kf
Which one of the links please?
https://forum.processing.org/one/topic/text-box-cursor-rendering-improvment.html
The second post which is by @calsign. I tested (not fully) and I believe it felt like in a text editor. Full tested is required though to see if it satisfy the generic needs and yours, of course.
Btw, in the link in the previous post, there is a controlP5 implementation. To clarify, it has some features of a text field but it is not a perfect piece. It is more to show other attempts and hoping maybe in the future see improvement in this control. However, I am not sure how much support you can get nowadays for that library.
Kf
Thanks for the replies. @kfrajer The solution offered in your first links are useful indeed and for example I can see how with string manipulation one can achieve something like pressing Backspace, but they dont account for arrows. The solution offered by @calsign seems to be excessively convoluted for the simple task of moving a text cursor. Here is the interesting thing though, there exists a getIndex() and a setIndex() method for the Textfiled class. I believe if I could use setIndex(), then I could move the cursor around. However, whenever I try using setIndex() I receive an error saying that " The method setIndex(int) from the type Textfield is not visible." And unfortunately I do not know how to make it visible. Any ideas?
setIndex? I don't see it in the documentation in the controlP5 javaDocs.
Kf
@kfrajer That is correct. It is not mentioned in the documentation of Textfield method either. However, if you have a look at the Textfield JAVA source file inside the library, setIndex(int) does exist and is used in the library code. Furthermore, setIndex(int) is defined as a "private" function (therefore not being visible as I pointed out earlier), which does not allow me to use it. Unfortunately, it seems that the fix is not as easy as just changing the "private" to "public" as the file needs to be compiled and etc. I am not sure how to go about that, but I have been reading some posts about it. I would appreciate it if you would have a look at the source file and confirm my understanding.
Java allows us to use any member of a class via reflection:
As GoToLoop mentioned, reflection is one way to do it. You can explore previous posts. As a matter of fact GoToLoop has contributed some examples using this approach.
https://forum.processing.org/two/search?Search=reflection
Kf