Results 1 to 3 of 3
  1. #1
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default stick caret in a postion jTextArea

    Hi everybody,
    So here's my problem I want the caret to stay in the same position as it was before the key up is pressed here's my code :
    Java Code:
     private void TrafficKeyPressed(java.awt.event.KeyEvent evt) { 
    int theCode=evt.getKeyCode();
    try {
                if ((theCode == KeyEvent.VK_UP)&& (Traffic.getCaretPosition() == (promptposition()+Traffic.getLineStartOffset(Traffic.getLineCount()-1))))
                { 
                        j +=1;
                        
                        Traffic.replaceRange(history[i-j], Traffic.getLineStartOffset(Traffic.getLineCount()-1) + promptposition(), Traffic.getLineEndOffset(Traffic.getLineCount()-1));
                        
                        Traffic.setCaretPosition(Traffic.getLineStartOffset(Traffic.getLineCount()-1)+ promptposition());
                        
                }
      
            }
            
             catch (BadLocationException ex) {
                Logger.getLogger(TrafficSerialPort.class.getName()).log(Level.SEVERE, null, ex);
            }
    So this code does the insert but the caret moves to the previous line.pls help

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: stick caret in a postion jTextArea

    My advice in your earlier thread still applies: InputStream TextField

    As for this question, DefaultCaret#setUpdatePolicy(...) may be useful.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: stick caret in a postion jTextArea

    Thanks for the answer but I wanted to find a simpler solution and I did :
    well I just added
    Java Code:
    evt.consome();
    before if and it works :)

Similar Threads

  1. Getting caret position from jtextarea
    By shivam0101 in forum AWT / Swing
    Replies: 2
    Last Post: 09-08-2012, 07:13 PM
  2. Replies: 2
    Last Post: 10-03-2011, 03:45 PM
  3. Read a Public Key from a USP stick
    By shauom in forum New To Java
    Replies: 2
    Last Post: 08-06-2008, 07:57 AM
  4. How to use TextLayout with Caret
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-25-2008, 10:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •