casper84mel
Programmer
Hi there;
I have a text field in which I want to be able to have control over the caret of the text field. e.g. when a left arrow key is pressed I want to be able to move the caret of the textfield to any position I want, rather to the next position in the field.
For example, a text field below, the caret is at the beginning of the field;
------------------------
|java-forum
------------------------
a press of left arrow key would normally set the positon after character 'j', before the character 'a'. But I want to make it move to the position just before the character 'f', so I want the text field to look like;
------------------------
java-|forum
------------------------
For this reason I extended the JTextField and implemented the KeyListener interface, and used the setCaretPosition method at the 'keyPressed' to set the caret to the position I want. But it seems that it moves the character to one more position, that it moves the caret to the position just after the character 'f'. The field looks like below;
------------------------
java-f|orum
------------------------
So does anybody know why this happens? I have searched for it and found out that the problem may be related about the defaultcaret behaviour, but I couldn't figure it out
Any help,idea,code piece will be greatly appreciated..
Thanks a lot
Cheers
I have a text field in which I want to be able to have control over the caret of the text field. e.g. when a left arrow key is pressed I want to be able to move the caret of the textfield to any position I want, rather to the next position in the field.
For example, a text field below, the caret is at the beginning of the field;
------------------------
|java-forum
------------------------
a press of left arrow key would normally set the positon after character 'j', before the character 'a'. But I want to make it move to the position just before the character 'f', so I want the text field to look like;
------------------------
java-|forum
------------------------
For this reason I extended the JTextField and implemented the KeyListener interface, and used the setCaretPosition method at the 'keyPressed' to set the caret to the position I want. But it seems that it moves the character to one more position, that it moves the caret to the position just after the character 'f'. The field looks like below;
------------------------
java-f|orum
------------------------
So does anybody know why this happens? I have searched for it and found out that the problem may be related about the defaultcaret behaviour, but I couldn't figure it out
Any help,idea,code piece will be greatly appreciated..
Thanks a lot
Cheers