• Post Reply Bookmark Topic Watch Topic
  • New Topic

selecting text in JTextPane(and finding it in the first place)  RSS feed

 
Randall Twede
Ranch Hand
Posts: 4589
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
assuming i have found the text i am looking for(i haven't), how do i select the text once i have found it. according to the API,JTextPane as the method public void select(int selectionStart,int selectionEnd). how do i get these ints?

String has the method public int indexOf(String str) i guess it returns the start number. should i be using Pattern and Matcher instead?
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you know the start you should be able to determine the end (based on the length of the search string).

How you get the start is up to your requirement.

1. Are you going to highlight word at a time or all occurrences at once? The indexof will return one at a time the Pattern.match will give all occurrences.

2. Will your users understand how to build a regex in your search field? Maybe you have a option for a simple string search and a regex search.

The real problem is getting the text from the text pane to use in your search. See Text and New Lines for the proper way to do this.
 
Randall Twede
Ranch Hand
Posts: 4589
6
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Rob for the excellent answer. normally i would be excited, but my laptop just quit working and i have lost the whole project because i didn't back up. well, i might be able to put my HD in another laptop
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!