aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes select a text from JTextArea  and chaange its color Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "select a text from JTextArea  and chaange its color" Watch "select a text from JTextArea  and chaange its color" New topic
Author

select a text from JTextArea and chaange its color

rohit rusty
Greenhorn

Joined: Jun 27, 2011
Posts: 15

hello friends,

i am trying to select a text from JTextArea i.e. txtInput and chaange its color to red. using the following statements

txtInput.select(startPos,startPos+inWord[i].length());
txtInput.setSelectedTextColor(Color.red);


but what actually happening is no text is getting selected and the color changes only when the text is manually selected! Please help

rohitrusty314
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2093
    
    7
Quit multi-posting questions. Your where given an answer to a related question in your last posting.
rohit rusty
Greenhorn

Joined: Jun 27, 2011
Posts: 15
Rob Camick wrote:Quit multi-posting questions. Your where given an answer to a related question in your last posting.


yes but i am not sure how to do it using JTextPane, plus i have also implemented some other actions using JTextArea.... here i am just not able to understand why the select(int,int) method is not working!
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2093
    
    7
Text selection is designed to be temporaray for all text components. The selection is only shown when the text component has focus.


You can't do permanent text coloring using a JTextArea unless you color the entire text as you noted in your last question.. A JTextArea wasn't designed for that purpose. A JTextPane WAS designed for that purpose.

rohit rusty
Greenhorn

Joined: Jun 27, 2011
Posts: 15
Rob Camick wrote:Text selection is designed to be temporaray for all text components. The selection is only shown when the text component has focus.


You can't do permanent text coloring using a JTextArea unless you color the entire text as you noted in your last question.. A JTextArea wasn't designed for that purpose. A JTextPane WAS designed for that purpose.



thankyou!!can you please give a little more detail on how to do that with JTextPane? it 'll be really really helpful.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2093
    
    7
I gave you the link to a tutorial which has a working example!
rohit rusty
Greenhorn

Joined: Jun 27, 2011
Posts: 15
Rob Camick wrote:I gave you the link to a tutorial which has a working example!


yes you did but what exactly i am trying to do here is (for now using JTextArea) to read the text written by the user using getText() method and then chose a particular word from thar text and cjange the color of only that chosen word to red(which is definately not working). i am not able to relate these operations to the JTextPane... it would be really really great if you could tell me how to begin with especially the Document class use i am not able to get that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: select a text from JTextArea and chaange its color
 
Similar Threads
Multi-color JTextAreas
Changing Color of a selectedtext in TextArea
JTextArea color
sql conversion to ql for Java/J2EE
JTextArea As JRadioButton