• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextPane -To find the index by providing xy position.

 
Sonara Rahul
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friends,
A text is set to JTextPane.Could u please help me out to find a word or character in that text by providing the x & Y position.How culd be this done when only xy position is known.
Eg:
This is sentence one.
The is sentence two.
I know the second lines x & y position but i need the index of that position(xy) in JTextPane.
Excepting u'r replies,
Sonara.
 
M.K.A. Monster
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can better post this one, in the Swing forum.
Regards,
Mark Monster
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is where this is moved to.
 
Ashish Mahajan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sonara,
int location = textPane.viewToModel(point);
// From this location in model one can easily find the word/text at that position

Hope this solves ur probs
 
Sonara Rahul
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Mr.Ashish,
Thanks very much for u'r reply.I'm able to find the location using viewToModel(point) ,but I'm not getting the word (which is very much needed),using that int value returned.Culd pl.. help me out how get the word using that location.
Thanks & Regards,
Sonara.
 
Ashish Mahajan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rahul,
First of all, don't be 2222 formal. All here r friends sharing thoughts & ideas.
Instead of spoon feeding, i'll rather give u some hints :-
1. Read java.text.BreakIterator javadocs
2. Take a look at Text Bound Demo
3. If u r not going for BreakIterator stuff then :-
After getting text of the document get it's char array and traverse back and forth to find the word boundaries. NOTE consider i18n issues and instead of checking for " " (space character) check whether the given character is space or not by Character.isWhiteSpace(char) method.
Note that the default BreakIterator instance assumes word boundary at every non letter and digit so there is high possibility that u may go for No. 3 option. NOTE that BreakIterator can be customized to suit any requirement.
Hope this will give u some dirn.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!