-
November 17th, 2006, 10:12 PM
#1
question regarding JFormattedTextField
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....
0 error(s), 0 warning(s)
-
November 18th, 2006, 06:44 AM
#2
Re: question regarding JFormattedTextField
You could try calling:
Code:
myFormattedTextField.setHighlighter(null);
Passing a null parameter disables highlighting.
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.
-
November 18th, 2006, 07:27 AM
#3
Re: question regarding JFormattedTextField
can i not let user click and change cursor position??
0 error(s), 0 warning(s)
-
November 18th, 2006, 08:07 AM
#4
Re: question regarding JFormattedTextField
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.
Last edited by keang; November 18th, 2006 at 08:08 AM.
Reason: typo
-
November 18th, 2006, 08:51 AM
#5
Re: question regarding JFormattedTextField
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..
0 error(s), 0 warning(s)
-
November 18th, 2006, 09:08 AM
#6
Re: question regarding JFormattedTextField
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.
Last edited by keang; November 18th, 2006 at 09:36 AM.
Reason: detail added
-
November 18th, 2006, 10:09 AM
#7
Re: question regarding JFormattedTextField
so it has no way to prevent changing of the caret position??
0 error(s), 0 warning(s)
-
November 18th, 2006, 10:21 AM
#8
Re: question regarding JFormattedTextField
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.
-
November 18th, 2006, 11:02 AM
#9
Re: question regarding JFormattedTextField
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
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
November 18th, 2006, 12:17 PM
#10
Re: question regarding JFormattedTextField
Originally Posted by dlorde
In your CaretListener, can't you just set the caret back to the desired position if there is no data entered?
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:
Code:
myFormattedTextField.addCaretListener(
new CaretListener() {
public void caretUpdate(CaretEvent e) {
if ( e.getDot() == 0 || e.getDot() == desiredLocation )
return;
((JFormattedTextField)e.getSource()).setCaretPosition(desiredLocation);
}
});
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|