Results 1 to 9 of 9
Thread: Coloring texts
- 01-14-2012, 12:27 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 28
- Rep Power
- 0
-
- 01-14-2012, 12:57 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 28
- Rep Power
- 0
Re: Coloring texts
JtextPane
- 01-14-2012, 01:59 AM #4
Re: Coloring texts
Have you looked at the API doc for the JTextPane? It has a link to the tutorial. In the tutorial are code examples of how to use the JTextPane.
- 01-14-2012, 04:05 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 28
- Rep Power
- 0
Re: Coloring texts
I did look and tried, i am not getting the colors on each line, are there any methods available to get the line number?
textEditor.setText("This is first line\nThis is second line\nThis is third line");
StyledDocument doc = textEditor.getStyledDocument();
Style styleRed = textEditor.addStyle("Red", null);
StyleConstants.setForeground(styleRed, Color.red);
Style styleGreen = textEditor.addStyle("Green", null);
StyleConstants.setForeground(styleGreen, Color.green);
lines = textEditor.getText().split("\n");
for(int i=0; i<lines.length; i++)
{
doc.setCharacterAttributes(0, 4, styleRed, true);
doc.setCharacterAttributes(4, 6, styleGreen, true);
}
- 01-14-2012, 04:32 AM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: Coloring texts
Why do you have a loop?
The point of using the setCharaterAttributes is to set the attributes for different characters in the the Document. You are attempting to set the first 5 characters to RED and the the next 2 characters to GREEN. There is no need to do this 3 times.
If you are having problems then post your Short, Self Contained, Correct Example. Don't forget to use the "code tags" so the code you post retains its formatting.
- 01-14-2012, 04:54 PM #7
Member
- Join Date
- Mar 2010
- Posts
- 28
- Rep Power
- 0
Re: Coloring texts
Yeah,
i want the first 5 chars to be red the next 3 char to be green of every line in the jtextpane.
- 01-14-2012, 05:11 PM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: Coloring texts
And you still haven't posted your SSCCE.
- 01-14-2012, 05:35 PM #9
Similar Threads
-
adding texts to show multiple texts in one JTextArea panel
By wndtjr2 in forum New To JavaReplies: 8Last Post: 11-05-2011, 04:20 PM -
SQL syntax coloring
By jperson in forum JDBCReplies: 4Last Post: 11-29-2010, 10:36 PM -
Coloring an image
By Cassidy in forum Java 2DReplies: 4Last Post: 05-31-2010, 09:09 PM -
Coloring JTable cells
By ProgrammingPup in forum Advanced JavaReplies: 2Last Post: 11-04-2009, 10:57 PM -
Coloring an image
By Twitchy5 in forum AWT / SwingReplies: 19Last Post: 09-12-2009, 03:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks