Reputation:
I want to make my error messages red, which are made using Swing:
JOptionPane.showMessageDialog(null,
"Welcome\nTo\nJava\nProgramming!", "subject", JOptionPane.ERROR_MESSAGE);
But this code will show a dialog box with black message, what's the way to make it red?
Upvotes: 0
Views: 2279
Reputation: 465
public void showColoredMessageDialog(String message, Color color){
//The label to show your message
JLabel l = new JLabel(message);
//the color for your message
l.setForeground(color);
//show JOptionPane.showMessageDialog with your custom color message
JOptionPane.showMessageDialog(this, l);
}
//You can call this method like for ex:
String msg = "hello world!";
//display this message with red color
showColoredMessageDialog(msg, Color.red);
//or you can also pass messages with variable values
int count = 10;
String msg2 = "the value of count is : " + count;
//display this message but this time with green color
showColoredMessageDialog(msg2, Color.green);
Upvotes: 0
Reputation: 10994
You can achieve it with help of HTML like next:
JOptionPane.showMessageDialog(null ,
"<html><div color=red>Welcome<br/>To<br/>Java<br/>Programming!" , "subject" , JOptionPane.ERROR_MESSAGE);
Upvotes: 5