• Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing the background color of a selected row in a JTextArea  RSS feed

 
Frankey James
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have the "DefaultEditorKit.selectLineAction" right now, and everything works fine, but I want to change the background color fo the selected text when the mouseevent fires. The text color Java is using is blue color and this is what I want changed to a different color.

Anyone have any idears?


 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Use a custom TableCellRenderer
2) Use the UIManager class to set the colors for the following properties:
- Table.selectionBackground
- Table.selectionForeground
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it really a JTextArea you're talking about?

if so, try the following line (before creation of textArea, and will affect all textAreas)

UIManager.put("TextArea.selectionBackground", new javax.swing.plaf.ColorUIResource(Color.YELLOW));
 
Frankey James
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:1) Use a custom TableCellRenderer
2) Use the UIManager class to set the colors for the following properties:
- Table.selectionBackground
- Table.selectionForeground


YOu know, I was going to ask about JTable as I thought perhaps this might be easiest done using that. However, I was unsure how to make a text editor (that's what i'm doing btw) using such a component...and, of course, didn't put much time into decideing jtextarea vs jtable vs jtextpane. Would a JTable be the best component to use for a text editor? I'm in the midst of reinventing the wheel as I'm trying to create something similar to Ultra Edit, but much less functionality.

Thanks for the response!
 
Frankey James
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:is it really a JTextArea you're talking about?

if so, try the following line (before creation of textArea, and will affect all textAreas)

UIManager.put("TextArea.selectionBackground", new javax.swing.plaf.ColorUIResource(Color.YELLOW));


Thanks, this does the trick, at least pretty good. What I really want is the entire row of text to stay highlighted after selection, even when moving the arrow keys so long as the keys don't change lines/row. I'm not sure how to go about this.

As stated in my previous post, I thought a JTable might do the trick, but I'm sure how to go about making new rows of text as a carriage return is entered...return key. Would I just respond to the RETURN button keyboard event, get the selected table row, create new blank row after current row and before next row, then get caret position and all text to the right of caret, and finally add that text to the new table row? Seems like a lot of work...or maybe there's a better way of doing this?
 
Frankey James
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wish I could re-do this thread and be more specific....

Requirements:
- single-click highlights row of text (got this already using Actions) to certain color (got that now with textArea.setSelectionColor() )
- selected row stays highlighted so long as caret does not does not leave row or mousevent doens't fire on another row (rows in jtextarea are simply new lines...so this may be impossible with jtextarea?)



Conclusion of previous posting to Michael (for future readers that might need to do this)....
So Michael showed me the UIManager, but I didn't realize it did the exact same thing as myTextArea.setSelectionColor(Color.Yellow), which is what I'm now using. Setting the color selection, using both setSelectionColor() OR UIManager.put, only works for double clicking, but I want it to work for single-clicking. This is where the Actions and mouseClicked event comes in handly, which is what I was using initially. Using this technique and either the UIManager.put(x,x) OR myTextArea.setSelectionColor(Color.Yellow) solves my problem of changing the color of text when selection happens and allows for single clicking.



edit: Sheesh....this was so dumb to do this in jtextarea or jtextpane. i am now using jtable and it's much easier to do what i want in just a matter of minutes (plus some) to get what i want, but took me hours to do in jtextarea. thanks for the input guys or gals...i'll create a new thread if i need more assistance.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
might get some ideas here

http://tips4java.wordpress.com/2008/10/29/line-painter/
 
Frankey James
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that looks really good. I tried working with Recangle myself before giving up and coming to this thread. ;-0 Still, I now thinkg a JTable may be best for a Text Editor. What do you think? I don't see a caretListenere, though...merry-go-round, maybe i might have to use the Rectangle to highlight a row.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Still, I now thinkg a JTable may be best for a Text Editor.

the two are chalk and cheese
 
Frankey James
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah. most other people i've seen use a jtextrea or jtextpane. i've not seen any examples of jtable used for java editor. i'll just have to mess with jtable more, otherwise result to using a rectangle to highlight a jtextarea row.

 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JTable confusion is my fault - I saw the combination of "selected row" and "JT", and automatically read JTable...

You could check out javax.swing.text.Highlighter in combination with a HighlighterPainter. Simply search for the start and end of the block to highlight, then call addHighlight on the Highlighter.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frankey James wrote:yeah. most other people i've seen use a jtextrea or jtextpane. i've not seen any examples of jtable used for java editor. i'll just have to mess with jtable more, otherwise result to using a rectangle to highlight a jtextarea row.


Most text editors don't have the concept of "row" at all. Like this box I'm typing into right now, which is pretty much equivalent to a Java JTextArea. The text is just assumed to be a long string of characters and it really doesn't matter that they are displayed in more than one line.

So if a line of text is significant in your application, probably you aren't writing a text editor. And therefore, probably a text component wasn't a good choice. You need something which keeps the lines from running into each other. JTable -- or JList if you only need one column -- might work better.
 
Frankey James
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:The JTable confusion is my fault - I saw the combination of "selected row" and "JT", and automatically read JTable...

You could check out javax.swing.text.Highlighter in combination with a HighlighterPainter. Simply search for the start and end of the block to highlight, then call addHighlight on the Highlighter.


I started with this the other day, but just didn't understand how to get it to work. I searched for a better example and found one here:
http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html

It's really what I need...because like a normal editor i want the highlighted row to span the width of the document (or at least longest row) and not just the area with text, so I need to control this aspect and not with some goofy single0click Action/mousevent like I was doing. Thanks for peeking my interest...it got me to try harder and find a result this time! ;-)



Paul - I'm not familiar enough with JTable, but at a quick glance it looks like it will be difficult to track the caret, which is anotehr requirement: i need to show row and column of caret (something like Ultra edit). If this can be overcome using JTable I agree this may have been the best solution, but i'm so far up shyte creek I won't be turning around any time soon now that i got the highlighter working....this is the last feature i needed to add. Thanks for the response.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!