Results 1 to 12 of 12
Like Tree5Likes
  • 1 Post By Norm
  • 1 Post By Norm
  • 1 Post By Norm
  • 1 Post By Norm
  • 1 Post By DarrylBurke

Thread: Change appearance of lines in a JTextArea

  1. #1
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default 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):

    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);
    				}
    Does this makes sense/is it the way to do it/is it possible to do it similar to this?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    JTextAreas are pretty plain. Everything is displayed the same.
    For individual character by character formatting, see the JTextPane class for example.
    blackbird likes this.

  3. #3
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    JTextAreas are pretty plain. Everything is displayed the same.
    For individual character by character formatting, see the JTextPane class for example.
    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);
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    give certain lines at least a slightly different look
    Make some lines all capital letters, otherwise
    JTextAreas are pretty plain. Everything is displayed the same.

    how to refer to a particular line/row in the JTextArea
    Read the API doc to see what methods are available.
    blackbird likes this.

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

    Default

    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)

  6. #6
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Make some lines all capital letters, otherwise
    [I]JTextAreas are pretty plain. Everything is displayed the same.
    That's an idea; it doesn't have to be fancy.


    Quote Originally Posted by Norm View Post
    Read the API doc to see what methods are available.
    I looked through all of the possible values in the editor and none looked all that likely.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Another simple idea: put a line of **********s before and after the line
    blackbird likes this.

  8. #8
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Another simple idea: put a line of **********s before and after the line
    Okay, once I figure out how to access a specific line...

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Lines end with the newline character. You either keep track as you add them, or count them starting at the beginning.
    blackbird likes this.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Thanks db. Learning new things every day.

  12. #12
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    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

  1. Text Antialiasing Appearance Inconsistent
    By neptune692 in forum Advanced Java
    Replies: 2
    Last Post: 04-28-2011, 08:45 AM
  2. [java3D] set Appearance again
    By Dennis in forum Advanced Java
    Replies: 0
    Last Post: 09-12-2010, 04:18 PM
  3. How to change the appearance of swing components?
    By XmisterIS in forum New To Java
    Replies: 4
    Last Post: 08-25-2010, 10:28 AM
  4. how to change the appearance of jbuttons
    By katie in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 10:26 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
  •