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

    Default Add text to JTextPane with color

    Hey, how can I add text with a color in jEditorPane1. I have
    Java Code:
        public void sM(String s, Color color){
        public void sM(String s, Color c){
            String text = jEditorPane1.getText();
            jEditorPane1.setForeground(c);
            jEditorPane1.setText(text + s + "\n");
    
        }
    and when I do
    Java Code:
            sM("Hi", Color.white);
            sM("Hi", Color.red);
    the color stays red?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    What is the type of your jEditorPane1?

    kind regards,

    Jos

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    Well, as a rule, JEditorPane should use some additional thing as Document to control its styles .
    Here is an example
    If that helps put (+1) for my rep. ;)
    JEditorPane example

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

    Default

    Read the JTextPane API and follow the link to the Swing tutorial on "Text Component Features" for a working example.

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

    Default

    With my pr0 google skills I have learned how to do it :).
    Thanks for your help.
    Java Code:
        public void sM(String s, Color c) {
    
                    // bad: instiantiates a new AttributeSet object on each call
            SimpleAttributeSet aset = new SimpleAttributeSet();
            StyleConstants.setForeground(aset, c);
    
            int len = jTextPane1.getText().length();
            jTextPane1.setCaretPosition(len); // place caret at the end (with no selection)
            jTextPane1.setCharacterAttributes(aset, false);
            jTextPane1.replaceSelection(s + "\n"); // there is no selection, so inserts at caret
    
        }
    That was it :)

Similar Threads

  1. text color
    By jpito in forum Advanced Java
    Replies: 1
    Last Post: 11-18-2009, 12:33 AM
  2. How to Get Text(RichText) from JTextPane
    By dorairaj in forum AWT / Swing
    Replies: 3
    Last Post: 07-15-2009, 07:33 AM
  3. Insert text at abitrary point in a JTextPane
    By angues in forum AWT / Swing
    Replies: 6
    Last Post: 02-02-2009, 04:08 PM
  4. color text in standalone???
    By hung_tyh in forum New To Java
    Replies: 6
    Last Post: 12-24-2008, 04:29 PM
  5. Changing the color of text
    By Lang in forum New To Java
    Replies: 1
    Last Post: 11-04-2007, 09:51 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
  •