Results 1 to 9 of 9

Thread: Coloring texts

  1. #1
    shivam0101 is offline Member
    Join Date
    Mar 2010
    Posts
    28
    Rep Power
    0

    Default Coloring texts

    Hi,

    I want to different color of different length in lines.
    forexample,
    This is a coloring example
    Thi = red color
    s is = blue color
    a coloring = orange color

    Can someone guide me?


    for(int i=0; i<lines.length; i++)
    {
    //iam using textpane to display the line.
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    shivam0101 is offline Member
    Join Date
    Mar 2010
    Posts
    28
    Rep Power
    0

    Default Re: Coloring texts

    JtextPane

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,773
    Rep Power
    25

    Default 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.

  5. #5
    shivam0101 is offline Member
    Join Date
    Mar 2010
    Posts
    28
    Rep Power
    0

    Default 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);
    }

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default 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.

  7. #7
    shivam0101 is offline Member
    Join Date
    Mar 2010
    Posts
    28
    Rep Power
    0

    Default 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.

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Coloring texts

    And you still haven't posted your SSCCE.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,773
    Rep Power
    25

    Default Re: Coloring texts

    every line in the jtextpane.
    Every line starts at a different offset in the text. You'll need a way to remember where the first character of each line is.

Similar Threads

  1. Replies: 8
    Last Post: 11-05-2011, 05:20 PM
  2. SQL syntax coloring
    By jperson in forum JDBC
    Replies: 4
    Last Post: 11-29-2010, 11:36 PM
  3. Coloring an image
    By Cassidy in forum Java 2D
    Replies: 4
    Last Post: 05-31-2010, 10:09 PM
  4. Coloring JTable cells
    By ProgrammingPup in forum Advanced Java
    Replies: 2
    Last Post: 11-04-2009, 11:57 PM
  5. Coloring an image
    By Twitchy5 in forum AWT / Swing
    Replies: 19
    Last Post: 09-12-2009, 04:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •