Reputation: 85
When working with JOptionPane in Java, how do I make a custom title on a message dialog? I currently have:
JOptionPane.showMessageDialog(null, "You won the game in " + tries + " tries!");
I tried to add another parameter, and it gave me an error. Any ideas?
Upvotes: 3
Views: 48220
Reputation: 161
I tried it this way:
JOptionPane.showMessageDialog(frame,message,title, JOptionPane.INFORMATION_MESSAGE);
where
JFrame frame = new JFrame();
String message = "Population: "
String title = "City Info:"
JOptionPane.INFORMATION_MESSAGE is messageType
Upvotes: 0
Reputation: 6516
Here is the correct syntax for a dialog with a title:
JOptionPane.showMessageDialog(null, "This is the message", "This is the title", JOptionPane.INFORMATION_MESSAGE);
// Component; Text to appear in window; Title; This is the type of dialog (can be error, as well)
Notice that there are four parameters, not three (there isn't a method with three parameters, as @Sanjay explained)
Upvotes: 6
Reputation: 12102
In case thoughts fail, we have JavaDocs to refer, which says,
showMessageDialog(Component parentComponent,
Object message,
String title,
int messageType)
Btw, your thought was right :) but note ,
+
but by ,
Upvotes: 4
Reputation: 694
Try this
JOptionPane.showMessageDialog(null, "You won the game in 7 tries!", "my title", JOptionPane.INFORMATION_MESSAGE);
You need to supply the message type (4th param) so swing know which default icon to display.
Upvotes: 17