Re: selection end and start
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)
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?
Re: selection end and start
Quote:
Originally Posted by
nasi
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.
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.
Re: selection end and start
helllooooooooooo, (think)
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).
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.
Code :
// 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
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".
Code :
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);
Re: selection end and start
Re: selection end and start
Quote:
Originally Posted by
nasi
@ 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".
Code :
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...
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.
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.