i have a formatted text field and when user did not enter anything, it can be highlighted and cursor can be shifted to middle of the mask.
how can i disable highlight and cursor shift when there is no input??
thanks in advance....
Printable View
i have a formatted text field and when user did not enter anything, it can be highlighted and cursor can be shifted to middle of the mask.
how can i disable highlight and cursor shift when there is no input??
thanks in advance....
You could try calling:Passing a null parameter disables highlighting.Code:myFormattedTextField.setHighlighter(null);
If you are going to need to renable highlighting at a later time I suggest you call getHighlighter() first and store the returned Highlighter object for later use.
can i not let user click and change cursor position??
I'm not sure what you mean by this because if the field is empty on my system you can't click and change the cursor position. It snaps back to the right hand side of the text field (assuming the alignment is set to RIGHT).
Having said that if you want the cursor to be at a specific position within the text then add a CaretListener by calling myFormattedTextField.addCaretListener(..). Your caret listener will get notified whenever the carat (cursor) position changes.
as i'm using JFormattedTextField with mask, even when nothing is type the cursor position still can be changed... i have no problem with JTextField.. :ehh:
Sorry, my mistake if it has a mask the user will be able to position the carat at any position within the mask. The carat listener will allow you to get notification of a change of carat position and hence to control its position.
so it has no way to prevent changing of the caret position??
Not that I'm aware of unless you want to disable the control totally which means that the user won't be able to enter text either.
In your CaretListener, can't you just set the caret back to the desired position if there is no data entered?
[i]Simplicity is prerequisite for reliability...[i]
E. Dijkstra
I've just tried this and you can as long as it's within the confines of the mask. The big problem is that caret events are generated on text entry and if you're not careful you end up with recursive calls when a key is pressed. But as long as you don't try to move a caret that is already at the desired location you are ok. I've also noticed another issue which is that entering the field via focus traversal causes a problem unless you discard the first event generated. I haven't worked out how to reliably do this yet and I've no more time now as I'm off to work. I crudely did it in my test code by ignoring events where the position was 0. Here's what I used:Quote:
Originally Posted by dlorde
Code:myFormattedTextField.addCaretListener(
new CaretListener() {
public void caretUpdate(CaretEvent e) {
if ( e.getDot() == 0 || e.getDot() == desiredLocation )
return;
((JFormattedTextField)e.getSource()).setCaretPosition(desiredLocation);
}
});