Results 1 to 11 of 11

Thread: JTextArea

  1. #1
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default JTextArea

    I have
    Java Code:
        public void sM(String s, Color c) {
            jTextArea1.setForeground(c);
            String textAreaText = jTextArea1.getText();
            jTextArea1.setText(textAreaText + s + "\n");
        }
    and when I do
    Java Code:
    sM("Hello", Color.red);
    sM("Hello", Color.blue);
    the text stays blue, but I want it to be in different colors.

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

    Default

    You'll probably need to delve into using either JTextPane or JEditorPane. The tutorials can show you how, but it's a bit more complex then using JTextAreas.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by PhQ View Post
    I have
    Java Code:
        public void sM(String s, Color c) {
            jTextArea1.setForeground(c);
            String textAreaText = jTextArea1.getText();
            jTextArea1.setText(textAreaText + s + "\n");
        }
    and when I do
    Java Code:
    sM("Hello", Color.red);
    sM("Hello", Color.blue);
    the text stays blue, but I want it to be in different colors.
    A JTextArea can have only one foreground color; changing it to red and immediately changing it to blue (again) leaves the forground color of the entire area to blue.

    kind regards,

    Jos

  4. #4
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    I think I am doing something wrong to my sM method, because when I do
    Java Code:
    sM("Hello", Color.red);
    sM("Hello", Color.blue);
    the text is like this :
    Hello
    Hello
    .
    But I want it to be like:
    Hello
    Hello.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by PhQ View Post
    I think I am doing something wrong to my sM method, because when I do
    Java Code:
    sM("Hello", Color.red);
    sM("Hello", Color.blue);
    the text is like this :
    Hello
    Hello
    .
    But I want it to be like:
    Hello
    Hello.
    Did you read my previous reply?

    kind regards,

    Jos

  6. #6
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    Did you read my previous reply?

    kind regards,

    Jos
    Lol, no.
    So what can I use to do this?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by PhQ View Post
    Lol, no.
    So what can I use to do this?
    Don't use a JTextArea but one of the decorated text panes instead: JTextPane or JEditorPane.

    kind regards,

    Jos

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

    Default

    Quote Originally Posted by PhQ View Post
    Lol, no.
    So what can I use to do this?
    We have already told you. Please re-read the first reply (and the other replies) in this thread.

    Much luck.
    Last edited by Fubarable; 03-28-2010 at 06:56 PM.

  9. #9
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    Quote Originally Posted by Fubarable View Post
    We have already told you. Please re-read the first reply (and the other replies) in this thread.

    Much luck.
    I have.

    I'm not sure how to write my method. Because if I do this :
    Java Code:
    jTextPane1.setText(jTextPane1.getText() + "\n" + jTextPane1.setForeground(c) + s );
    I get an error.

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

    Default

    You will have to study the tutorials. As I noted, using a JTextPane or JEditorPane is not as straight forward as using JTextArea as I believe that you might need to use SytledDocuments, AttributeSets, EditorKits, etc... (it's been a while since I've looked at this). You may also be able to get different colors by using HTML tags, but I've not tried this myself. The tutorials and the API can get you started, but plan on needing a little time to study it before you can get it implemented.

    Again much luck.

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by PhQ View Post
    I have.

    I'm not sure how to write my method. Because if I do this :
    Java Code:
    jTextPane1.setText(jTextPane1.getText() + "\n" + jTextPane1.setForeground(c) + s );
    I get an error.
    I wonder if you actually did read all replies. Well, did you? If so why are you stubbornly trying to accomplish something that can only be done that (ludicrous) way in your imagination?

    kind regards,

    Jos

    ps. A Color is not a String, nor will a JTextArea understand it, and read the API to see if a setForeground( ... ) method returns a value. hint: it doesn't so it can't be used in way you are trying to do.

Similar Threads

  1. JTextArea on PopUp -JTextArea isn't editable
    By Richy76 in forum AWT / Swing
    Replies: 3
    Last Post: 02-01-2010, 08:51 PM
  2. adding jtextarea
    By xhoneyskye in forum AWT / Swing
    Replies: 4
    Last Post: 01-26-2010, 04:46 AM
  3. Tab key in JTextArea
    By KristoZ in forum New To Java
    Replies: 1
    Last Post: 09-25-2009, 08:27 PM
  4. About JTEXTAREA
    By makpandian in forum AWT / Swing
    Replies: 4
    Last Post: 03-19-2009, 07:53 AM
  5. JTextArea
    By saytri in forum New To Java
    Replies: 0
    Last Post: 01-13-2008, 02:07 AM

Posting Permissions

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