• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTextPane -To find the index by providing xy position.

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you can better post this one, in the Swing forum.
Regards,
Mark Monster
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is where this is moved to.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic