Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how do I add colors to strings

Posted on 2004-10-28
7
Medium Priority
?
208 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
Comment
Question by:azcalv408
7 Comments
 
LVL 9

Expert Comment

by:Nick_72
ID: 12436160
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:
CEHJ earned 200 total points
ID: 12436171
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
ID: 12436274
i want to show it within a Jpanel. thanks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

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

Expert Comment

by:Nick_72
ID: 12436417
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
ID: 12436457
8-)
0
 
LVL 24

Expert Comment

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

916 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