Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: selection end and start

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default selection end and start

    I am using the following code but "end" and "start" is not exactly the "end" and "start" of the selected text in JeditorPane.
     
    JEditorPane note = new JEditorPane();
    int end;
    int start; 
     
    public void GetText() throws Exception{
     
    		query = note.getSelectedText();
    		end=note.getSelectionEnd();
    		start=note.getSelectionStart();
     
    	}


  2. #2
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: selection end and start

    no answer?

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: selection end and start

    What do you mean by 'not exactly'? If it is off by 1 you must remember that the selections are zero offset (eg getSelectionStart() from a text component whose selection starts at the beginning of the document would return 0)

  4. #4
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: selection end and start

    no, it is more than just 1. what is the number that is returned by getSelectionStart() or getSelectionEnd? I mean, is it the X coordinate of selected text, is it its Y coordinate or anything else?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: selection end and start

    Quote Originally Posted by nasi View Post
    no, it is more than just 1. what is the number that is returned by getSelectionStart() or getSelectionEnd? I mean, is it the X coordinate of selected text, is it its Y coordinate or anything else?
    It is the start and end indexes of the selected text. eg consider the string "string", if 'str' is selected in the TextComponent, then getSelectionStart() should return 0. NOTE: if you are displaying an html string in the component, the numbers returned by these methods will not match up to the original string representation of the html String.

  6. #6
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: selection end and start

    Thanks. yes I am working on html String. So what should I do if I want to open a frame exactly at the end of selected text? I know that I should use setBounds() Method, but I don't know how to set the position of the frame to the end of selected text.

  7. #7
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: selection end and start

    helllooooooooooo, (think)

  8. #8
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: selection end and start

    The selection start & end gives you the character position in the text, not a screen location. To get a screen location, use the modelToView(..) method, which will give you the location in the viewport. You can then use this to get the appropriate location relative to, e.g. the frame window or the screen (with SwingUtilities.convertRectangle(..), or the convertPoint(..) methods).

  9. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: selection end and start

    Please be patient, we're not always able to answer your questions right away

    Do you only want the text selected from the HTML source, or the rendered output of the HTML?

    You can change the EditorKit of the JEditorPane to accommodate which situation you want.

    // sets the selection to the bounds of only the rendered output
    		JEditorPane note = new JEditorPane();
    		note.setEditorKit(new HTMLEditorKit());
    		note.setText("<br>Hello world</br>");
    		note.setSelectionEnd(note.getText().length());
    		System.out.println(note.getSelectionEnd());      // prints 13 because the HTML tags have been parsed away and are not displayed
    		note.setEditorKit(new DefaultEditorKit());
    		note.setText("<br>Hello world</br>");
    		note.setSelectionEnd(note.getText().length());
    		System.out.println(note.getSelectionEnd());      // prints 20 because it's including the HTML source

  10. #10
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: selection end and start

    @ helloworld922: I am quite patient, I just want to remind you.

    @ dlorde, thanks ,I am using the following code but it doesn't convert the y coordinate from "splitPane" to " note". I mean that the Y coordinate is relative to "splitlPane" and not "note".

    JEditorPane note = new JEditorPane();
    JScrollPane paneScrollPane = new JScrollPane(note);
    JSplitPane  splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
    					upPane,
    					paneScrollPane);
     
    SwingUtilities.convertRectangle(splitlPane,note.modelToView(note.getSelectionEnd()),note);
    Last edited by nasi; May 7th, 2010 at 08:26 PM.

  11. #11
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: selection end and start

    ???????????????????????
    Last edited by nasi; May 8th, 2010 at 07:51 AM.

  12. #12
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: selection end and start

    Quote Originally Posted by nasi View Post
    @ dlorde, thanks ,I am using the following code but it doesn't convert the y coordinate from "splitPane" to " note". I mean that the Y coordinate is relative to "splitlPane" and not "note".

    JEditorPane note = new JEditorPane();
    JScrollPane paneScrollPane = new JScrollPane(note);
    JSplitPane  splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
    					upPane,
    					paneScrollPane);
     
    SwingUtilities.convertRectangle(splitlPane,note.modelToView(note.getSelectionEnd()),note);
    Er, the first argument should be the source component and the last argument should be the destination component - the JSplitPane is not the editor pane view component - but why would you want to convert the view co-ordinates to the JEditorPane co-ordinates? For a visible location they're going to be pretty much the same. If you want to display a frame at that location, don't you need co-ordinates relative to component that will display the frame (e.g. the main frame), or perhaps the absolute screen co-ordinates?

    Maybe I didn't understand quite what you're trying to do...

  13. #13
    Member
    Join Date
    Mar 2010
    Posts
    111
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: selection end and start

    As it is shown in my code the main frame is devided to 2 parts horizentally and the JEditorPane is the lower part of JSplitPane. the coordinates that are generated by SwingUtilities.convertRectangle(...) are relative to the main frame(JSplitPane), I need them to be generated relative to JEditorPane. I highlight a word in JEditorPane and I need a new frame opens exactly at the end of the selected word but now the new frame is created upper than the selected word, and I think that the reason is that on the top of main frame is a panel which is not part of JEditorPane. meanwhile I changed the source component to main frame but no changes happened.

  14. #14
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: selection end and start

    The source component is the component you want to convert the co-ordinates from (e.g. editor pane view). The destination component is the component you want to convert the co-ordinates to (e.g. main frame or whatever context you'll create the frame in).

    See ConvertRectangle Examples.
    Last edited by dlorde; May 11th, 2010 at 05:14 AM.

Similar Threads

  1. Where to start?
    By tonykasdorf in forum Java Theory & Questions
    Replies: 3
    Last Post: March 4th, 2010, 11:52 PM
  2. Selection Sorting of Data type (Char)
    By chronoz13 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 08:28 PM
  3. Selection Sorting
    By chronoz13 in forum Algorithms & Recursion
    Replies: 5
    Last Post: December 10th, 2009, 11:08 AM
  4. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM
  5. How should i write and compile java with Ubuntu?
    By Talk Binary in forum Java IDEs
    Replies: 19
    Last Post: May 7th, 2009, 05:29 AM

Tags for this Thread