Jerryg225
Jerryg225

Reputation: 85

Custom title on JOptionPane message dialogs

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

Answers (4)

DISHA
DISHA

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

fireshadow52
fireshadow52

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

COD3BOY
COD3BOY

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 ,

  1. There is no method with three parameters, but it have four parameter and yes the third one is the title for the dialog as String.
  2. The parameters are not separated by + but by ,

Upvotes: 4

user931366
user931366

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

Related Questions