CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 2003
    Posts
    162

    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)

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.

  3. #3
    Join Date
    Aug 2003
    Posts
    162

    Re: question regarding JFormattedTextField

    can i not let user click and change cursor position??
    0 error(s), 0 warning(s)

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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

  5. #5
    Join Date
    Aug 2003
    Posts
    162

    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)

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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

  7. #7
    Join Date
    Aug 2003
    Posts
    162

    Re: question regarding JFormattedTextField

    so it has no way to prevent changing of the caret position??
    0 error(s), 0 warning(s)

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.

  9. #9
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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.

  10. #10
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: question regarding JFormattedTextField

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured