Solved

how do I add colors to strings

Posted on 2004-10-28
207 Views
Last Modified: 2010-03-31
Hi,

    I'm new to Java. Can someone tell me how I can change a string to different colors ? Thanks!
    ex:   String s = "Print this line in red";
0
Question by:azcalv408
    7 Comments
     
    LVL 9

    Expert Comment

    by:Nick_72
    It's not the actual string that has the color property. It's the container in which you present the string. Where are you going to show it?
    0
     
    LVL 86

    Accepted Solution

    by:
    That depends on the context. This is one way

    JEditorPane ep = new JEditorPane();
    ep.setContentType("text/html");
    ep.setEditable(false);
    ep.setText("<font color=\"red\">" + s + "</font>");
    0
     

    Author Comment

    by:azcalv408
    i want to show it within a Jpanel. thanks
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    Add the JEditorPane to the JPanel
    0
     
    LVL 9

    Expert Comment

    by:Nick_72
    Or add a JLabel to the JPanel:

    JLabel label = new JLabel();
    JPanel.add(label);
    label.setForeground(Color.RED);
    label.setText(theString);
    0
     
    LVL 86

    Expert Comment

    by:CEHJ
    8-)
    0
     
    LVL 24

    Expert Comment

    by:sciuriware
    Almost all JAVA SWING controls (labels, buttons ...) can display HTML texts.
    ;JOOP!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    struts spring hibernate example 12 43
    no14 challenge 14 39
    Receive file in Servlet 1 15
    array11 challenge 16 18
    1. Package the applet into a JAR file. The applet must be in a JAR file before a certificate can be attached to it. Use the jar JDK utility. If the applet was previously referenced with the help of a codebase attribute in  tag, replace the codebase …
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now