How to change the color of a String

Posted on 2003-11-09
Last Modified: 2010-03-31
I made a String and I want to show it in a JOptionPane().  But I don't know how to change the color of that String (or SrtingBuffer)
Question by:Katze
LVL 15

Accepted Solution

jimmack earned 75 total points
ID: 9711445
You could put it inside a JLabel.  Then you can include HTML.

JLabel coloredString = new JLabel("<HTML><BODY><FONT COLOR='#FF0000'>" + myString + "</FONT></BODY></HTML>");

Expert Comment

ID: 9711628
You could also make it a borderless button and set the foreground color:

  String myString = new String("whatever");
  JButton myButton = new JButton(myString);

Assisted Solution

vk33 earned 50 total points
ID: 9712640
Hi! You can set foreground for ANY JComponent. So, you can set foreground for your JLabel:

JLabel label = new JLabel("Hello!");
label.setForeground (Color.RED);
JOptionPane.showMessageDialog(null, label);

Good luck!

Author Comment

ID: 9718136
Thanks to all of you!!

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Understanding websocket example in spring 1 42
topping1 challenge 7 51
Java and GPO 11 47
github account with ecipse 1 17
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

706 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

15 Experts available now in Live!

Get 1:1 Help Now