wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Setting icon/image on Title of JOptionPane.showConfirmDialog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Setting icon/image on Title of JOptionPane.showConfirmDialog" Watch "Setting icon/image on Title of JOptionPane.showConfirmDialog" New topic
Author

Setting icon/image on Title of JOptionPane.showConfirmDialog

Sham Phadtale
Ranch Hand

Joined: Jul 29, 2008
Posts: 75
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

Joined: Oct 13, 2005
Posts: 40061
    
  28
Have you been through the Java Tutorials and the JOptionPane documentation? There is lots of useful information there.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19794
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sham Phadtale
Ranch Hand

Joined: Jul 29, 2008
Posts: 75
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

Joined: Oct 27, 2005
Posts: 19794
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting icon/image on Title of JOptionPane.showConfirmDialog