Text Searching


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 1 of 1

Thread: Text Searching

Threaded View

  1. #1
    Join Date
    Oct 2004
    Posts
    151

    Question Text Searching

    Hi everyone,

    I have a textpane with some text inside and trying to search for a particular text in the textpane by highlighting it. Basically i the main function that does this is as follows

    TextPane is an instance of my JTextPane

    Code:
    public void findtext(int pos1, String str1)
    {
    //This function finds the specified text in the document in the JTextPane
    //according the downwards position of the caret in the JTextPane 
    
            Document Document1 = null;
            String str2 = null;
            int w = 0, w1 = 0, w2 = 0;
    
    //The below command line gets the document of the JTextPane
    
        Document1 = TextPane1.getDocument();
        System.out.println("" + pos1);
    try
    {
    //The below command line gets the text of the document according to 
    //the downwards position of the caret in the JTextPane
    
     str2 = Document1.getText(pos1, Document1.getLength() - pos1);
    //The below two command lines converts the text in the document and the 
    //the text to be searched both to lower case letters
    
    //str1 = str1.toLowerCase();
    //str2 = str2.toLowerCase();
    //The below command line gets the length of the text to be searched
    
              w2 = str1.length();
    }
    
    catch(Exception e)
    {
             Label2.setText("A caret position error has occurred");
    }
    
    //The below command line gets the index of the searched text in the document
    
        w = str2.indexOf(str1);
    
    if(w == -1)
    {
    //In this if statement TextPane1 is gained focus when the searched text
    //is not found in the document and the function is returned
    
       TextPane1.grabFocus();
      Label2.setText("The text that was searched was not found");
      return;
    }
    
    else
    {
      Label2.setText("");
    //In this else statement if the searched text is found in the document 
    //the text in the document is selected with respect to the current 
    //caret position in the JTextPane
    
      w1 = (w + pos1);
      TextPane1.select(w1, w1 + w2);
      TextPane1.grabFocus();
    }
    
    }
    The problem is that the first time that i search the jtextpane the above function works Ok but when i try to do it again only the last text in the jtextpane is selected

    I really do not know what is wrong and hope someone can help me with this problem.

    Any help is greatly appreciated

    Thank You

    Yours Sincerely

    Richard West
    Last edited by freesoft_2000; 12-04-2004 at 03:32 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center