Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java GUI
|
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 30th, 2003, 08:55 PM
Authorized User
 
Join Date: Oct 2003
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mrafaqi Send a message via Yahoo to mrafaqi
Default How a Specific word in JTextArea can be selected

hi!
   i want help that how a specific word in JTextArea is selected means highlighted, as in most text editor after finding a specific word occurs. if any body know tell me and thanks for this.
                                      MUHAMMADrizwanAKRAMZIA


__________________
ALI_R
 
Old February 2nd, 2005, 10:08 PM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Assuming your JTextArea is called display:

public void findText(String findThis)
    //Find the specified string, and highlight it
    /**@post findThis is highlighted if found in text area
       @param String findThis - the String to find*/
    {
        int currentPos=display.getCaretPosition();
        int docLength=getText().length();
        setSelectedText(0,0);
        if(getText().indexOf(findThis,currentPos+1)>-1) {
            display.setCaretPosition(getText().indexOf(findThi s,currentPos+1));
            setSelectedText(display.getCaretPosition(), ( display.getCaretPosition() + findThis.length() ) );
        }
        else {
            if(getText().indexOf(findThis)>-1) {
                display.setCaretPosition(getText().indexOf(findThi s));
                setSelectedText(display.getCaretPosition(), ( display.getCaretPosition() + findThis.length() ) );
            }
        }
    }

(more context on this method at: http://www.charlieharvey.com/java/source/NotePad.java )

HTH
charlie


--
Don't Stand on your head - you'll get footprints in your hair. http://charlieharvey.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
a bold word in a selected sentence Birger XSLT 4 January 1st, 2007 07:02 PM
Printing specific pages from Word 2000 jpmhughes Beginning VB 6 5 April 30th, 2005 06:35 PM
How a particular word in JTextArea is selected mrafaqi Java GUI 1 April 11th, 2004 09:14 AM
Heighlet Specific Word when open file in a browser salman Classic ASP Databases 1 March 30th, 2004 06:30 AM
Urgent JTextArea help Bloedbyl Java GUI 1 March 21st, 2004 06:08 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.