Results 1 to 12 of 12
5Likes Thread: Change appearance of lines in a JTextArea
- 07-08-2011, 09:52 PM #1
Senior Member
- Join Date
- Jul 2011
- Posts
- 100
- Rep Power
- 0
Change appearance of lines in a JTextArea
I have a JTextArea that will send its contents over a socket one line at a time. As the lines are sent, I want them to change appearance, perhaps by going from black font to gray, or changing background/foreground color.
Is there a common "GUI pattern" for this?
I will be looping through the JTextArea, so right after sending the line off into the ether, I should be able to tweak its representation. I'm thinking something like this (pseudocode - "outgoing" is the name of the JTextArea):
Does this makes sense/is it the way to do it/is it possible to do it similar to this?Java Code:for (int i = 0, i < outgoing.size, ++) { writer.println(outgoing[i].getText()); outgoing[i].background = Color.black; outgoing[i].foreground = Color.white; sleep(10); }
- 07-08-2011, 10:10 PM #2
JTextAreas are pretty plain. Everything is displayed the same.
For individual character by character formatting, see the JTextPane class for example.
- 07-08-2011, 10:17 PM #3
Senior Member
- Join Date
- Jul 2011
- Posts
- 100
- Rep Power
- 0
OK, I'll czech that out.
Is it possible, though, to change the JTextArea at all to give certain lines at least a slightly different look?
This pseudocode is closer, but I still don't know how to refer to a particular line/row in the JTextArea
Java Code:for (int i = 0; i < outgoing.getLineCount(); i++) { writer.println(outgoing.getLine(i)); outgoing[i].setBackground = Color.black; outgoing[i].setForeground = Color.white; Thread.sleep(100); }
- 07-08-2011, 10:47 PM #4
Make some lines all capital letters, otherwisegive certain lines at least a slightly different look
JTextAreas are pretty plain. Everything is displayed the same.
Read the API doc to see what methods are available.how to refer to a particular line/row in the JTextArea
- 07-08-2011, 10:56 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,146
- Rep Power
- 5
A JList might be a better solution. You can use a custom renderer.
See: How to Use Lists (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
Otherwise you would need to use a JTextPane which does support different fonts and colors:
See: Text Component Features (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
- 07-08-2011, 11:02 PM #6
Senior Member
- Join Date
- Jul 2011
- Posts
- 100
- Rep Power
- 0
- 07-08-2011, 11:06 PM #7
Another simple idea: put a line of **********s before and after the line
- 07-08-2011, 11:10 PM #8
Senior Member
- Join Date
- Jul 2011
- Posts
- 100
- Rep Power
- 0
- 07-08-2011, 11:12 PM #9
Lines end with the newline character. You either keep track as you add them, or count them starting at the beginning.
- 07-09-2011, 08:44 AM #10
Use a Highlighter. See the example here: How to Use Text Fields (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
db
- 07-09-2011, 01:51 PM #11
Thanks db. Learning new things every day.
- 07-11-2011, 05:58 PM #12
Senior Member
- Join Date
- Jul 2011
- Posts
- 100
- Rep Power
- 0
The "Example: Getting and setting all lines sequentially" section here: Java: JTextArea seems to be purt near what the non-doctor ordered:
Java Code:String text = inputArea.getText(); int totalLines = inputArea.getLineCount(); for (int i=0; i < totalLines; i++) { int start = inputArea.getLineStartOffset(i); int end = inputArea.getLineEndOffset(i); String line = text.substring(start, end); outputArea.append(line + "\n"); }
Similar Threads
-
Text Antialiasing Appearance Inconsistent
By neptune692 in forum Advanced JavaReplies: 2Last Post: 04-28-2011, 08:45 AM -
[java3D] set Appearance again
By Dennis in forum Advanced JavaReplies: 0Last Post: 09-12-2010, 04:18 PM -
How to change the appearance of swing components?
By XmisterIS in forum New To JavaReplies: 4Last Post: 08-25-2010, 10:28 AM -
how to change the appearance of jbuttons
By katie in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 10:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks