Results 1 to 6 of 6
  1. #1
    helloworld111 is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default get position in string from caret position

    Hello,

    Maybe there's some simple solution to this but I've been searching for some time and couldn't find any:

    When you read getCaretPosition from JEditorPane, the position will not correspond to the string's index from getText, if you use "text/html" to display HTML.

    E.g. if you have the text "<html>hello</html>" and the cursor is between the h and e, the caret position will be 2, but the index of the 'e' in the string is 7.

    How can I convert from the caret position to the corresponding position of the letter to the right of the caret, within the string (so, if the cursor is between the h and e, how do I get the correct index for e within the string returned by getText)?

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Unless someone else has used JEditPane more than I, all I suggest are two things.

    First, check the API methods carefully, including parent components, to see if there is a special method for this problem.

    Second, move the String to a char[]. Create two indexes, one to move through the array, the second to count only non-markup characters. Move through the array, update the second index only when you are not between < and >, inclusive. Remember to check for escape characters "\" (they should not be counted), and to count escaped "<>".

  3. #3
    helloworld111 is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    Hello Steve,

    I couldn't find anything in the API. I was working on the second method you suggested, but it probably gets more complicated if you also use stuff like &nbsp;.

    That's why I was wondering of someone had already build something like this.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Where "pane" is a JEditorPane:
    Java Code:
    Document doc = pane.getDocument();
    Caret caret = pane.getCaret();
    int dot = caret.getDot();
    String docText = doc.getText(0, doc.getLength());
    String nextCharacter = docText.substring(dot, dot + 1);
    System.out.println(nextCharacter);
    Adapt that to your need.

    db

  5. #5
    helloworld111 is offline Member
    Join Date
    Feb 2009
    Posts
    5
    Rep Power
    0

    Default

    I should have explained what I actually want to do:
    When a user puts the cursor in the non-editable JEditorPane, I want to add some text at the end of the line where the user clicked (so the first occurence of \n or <br> after the caret position).
    This is why I would like to know the caret position within the model (i.e. the string), and not the caret position on screen.
    I don't think you can do it with the standard methods, but perhaps someone has build and published a util class to do this?

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    > I should have explained what I actually want to do:
    Yes, you should have.

    Utilities (Java Platform SE 6)

    db

Similar Threads

  1. How to request position to the satelite?
    By vandawaa in forum Sun Java Wireless Toolkit
    Replies: 3
    Last Post: 01-11-2009, 04:41 PM
  2. How to request position to the satelite?
    By vandawaa in forum New To Java
    Replies: 0
    Last Post: 01-10-2009, 05:21 PM
  3. BorderFactory to set position?
    By aleplgr in forum AWT / Swing
    Replies: 2
    Last Post: 01-30-2008, 01:46 PM
  4. Help with position in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:54 PM
  5. Use the mouse position
    By susan in forum Java Applets
    Replies: 1
    Last Post: 07-29-2007, 12:10 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
  •