views:

230

answers:

3

Hi all, I'm trying to make one word (variable) of a message box bold in my Java program. Here is my code:

int n = messageBox.showConfirmDialog(frame,
"The File "+ file +" already exists." +
"\n" + "Do you want to replace it?",
"File Already Exists!",
messageBox.YES_NO_OPTION);

I want to make the variable "file" appear in bold text in my message box. So far I have only been able to get the entire message box to appear in bold, or none of it at all. How do I do this?

+1  A: 

Try wrapping your text in html tags. Many of the swing components support some basic HTML such as italic, bold and underlining. For example, you should change your code to read:

int n = messageBox.showConfirmDialog(frame,
"<html>The File <b>"+ file +"</b> already exists." +
"\n" + "Do you want to replace it?</html>",
"File Already Exists!",
messageBox.YES_NO_OPTION);
Richie_W
Good idea, but it does not work here, even with javax.swing.text.html.HTML.Tag imported.
typoknig
What OS/JRE are you using? This statement works for me (on Vista/JRE 1.6_u13).
Ash
My OS is W7X64. I have JDK 1.6.0_17 installed for both 32 and 64 bit and (both of which come with their respective versions of JRE 1.6.0_17). I am using NetBeans and it is relying on the 32 bit JDK and JRE.
typoknig
Thanks for your help, your answer was right on, I just didn't realize my text was bold by default in the message window. I just italicized instead.
typoknig
+2  A: 

Using HTML works fine for me. The problem is that the default font is already bold so you don't see a different.

Try using an "italic" tag or maybe a "font" tag and specify a different color to see the difference.

Or instead of passing in a text String, you can pass in your own JLabel with a custom font. Something like:

String message = "<html>The File <b> file </b> already exists</html>";
JLabel label = new JLabel(message);
label.setFont( UIManager.getFont("TextField.font") );

int result = JOptionPane.showConfirmDialog(
    this,
    label,
    "File already exists!",
    JOptionPane.YES_NO_OPTION);
camickr
Haha, good catch! I didn't even realize the text was already in bold! Thanks everyone!
typoknig
So I solved your problem and you accept the other answer?
camickr
lol - camickr, its just bad luck to get a douche-bag who doesn't obey the etiquette of SO.
Chii
Technically the other guy did solve my problem, I just didn't realize the text was already bold. If I could accept both answers I would. Since you care about points so much I will remember you and when I get enough points to give away you can have as many aw you want... I think you have to wait till I get 50 though (unless you know another way). Thanks for your help.
typoknig
Really? Your reply was "it does not work here". So if I hadn't explained the problem and proposed other solutions you would still be guessing. Points is not the issue, they are worth nothing.
camickr
Oh man, Camickr, just thought I'd tell you that those points felt good. Yum yum, so filling. :)
Richie_W
Carmickr, if points are not the issue what are you still writing for? You both helped me, and isn't helping other people with there code what this site is about? The world would be a pretty crappy place if people only helped others to get something in return. Again, I do appricate your contribution, as it probably would have taken me a bit longer to figure out I just needed to italicize instead.
typoknig
I just find so many people in the forum only read and accept the first answer that posts code. It gets frustrating when people don't take the time to completely understand the suggestions made. As it is your solution is to use italized text. That is NOT your original requirment. I showed you how to satisfy your original requirement and tried to get you thinking outside the box at the same time by showing you how flexible a JOptionPane can be.
camickr
Haha, this is hilarious.
Richie_W
A: 

it is working in java jdk1.6.0_21??

denmark