Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

select a text from JTextArea and chaange its color

 
rohit rusty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 2608
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quit multi-posting questions. Your where given an answer to a related question in your last posting.
 
rohit rusty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2608
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2608
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave you the link to a tutorial which has a working example!
 
rohit rusty
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic