Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting icon/image on Title of JOptionPane.showConfirmDialog

 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using -

JOptionPane.showConfirmDialog(frame, message, "Error",JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE);

I want set some image on Title of this box. Please anyone can tell me how to do this.
I tried by setting image on frame(parent componenet) but it did not work.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you been through the Java Tutorials and the JOptionPane documentation? There is lots of useful information there.
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what Sham wants isn't in there, because it's usually not something you modify manually. If I understand correctly, it's not the icon in the dialog's content pane but the icon at the top-left of the dialog's title bar - the bar with the little close button on the right.

It's possible to change this, but not using the static JOptionPane methods. You need to:
1) create a JOptionPane instance, with the appropriate arguments.
2) call createDialog on that instance, with the appropriate arguments.
3) call setIconImage on the created JDialog to change the icon.
4) show the dialog (setVisible(true)).
5) get the JOptionPane's value (getValue()).
6) compare the value (as an Integer by default) against the possible values (JOptionPane.OK_OPTION, etc). Beware of null, as that indicates the dialog was closed using the close button.
7) do something based on that value.

Steps 5-7 are not needed if you only need to show a message with a single OK button.
 
Sham Phadtale
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob!!
This is the thing for which I was looking. You have given answer in very good fashion. No chances of missing anything. It worked for me.
Thanks again.
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
It's a shame that JOptionPane is so limited sometimes. By default it also uses application modality, so in a project where I needed document modality (check out ModalityType for more info) I had to create all the dialogs like this as well, so I could manually change the modality.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic