views:

40

answers:

4

Hi,

I'm trying to use a custome icon in the title of the JOption pane rather then with the label. Is there a way that I can do that? I'm using the Icon class (as shown below) in JOptionPane but it keeps displaying the icon in the main display area rather than in the title. Here is the code:

Icon icon = new ImageIcon(ApplicationManager.getApplicationImage().getImage());
String jid = (String)JOptionPane.showInputDialog(ApplicationManager.getMainWindow(), 
                 Res.getString("label.enter.address"), 
                 Res.getString("title.start.chat"), 
                 JOptionPane.QUESTION_MESSAGE, 
                 icon,                    
                 null, 
                 selectedUser);

Thanks

+1  A: 

Haven't tried it, but you might get it to work with an internal frame, rather than using a dialog box. Try creating an instance of JOptionPane and call getInternalFrame(). JInternalFrame has a setFrameIcon(Icon icon) method.

Edit: On course the JInteralFrame's parent must be a JDesktopPane, but apart from this it should work.

Qwerky
@Qwerky This would not work as I have some pre selected value that I need to populate the Input dialog box with and hence cant use the JOptionPane constructor but instead have to use showInputDialog method..Thanks.
Global Dictator
+1  A: 

Try this code..

import javax.swing.JOptionPane;
import javax.swing.JDialog;
import javax.imageio.ImageIO;
import java.awt.Image;
import java.net.URL;

class OptionPaneIcon {

    public static void main(String[] args) throws Exception {
        JOptionPane jop = new JOptionPane(
            "Message",
            JOptionPane.QUESTION_MESSAGE,
            JOptionPane.DEFAULT_OPTION
            );

        JDialog dialog = jop.createDialog("Dialog Title");

        Image image = ImageIO.read(new URL(
            "http://www.gravatar.com/avatar/f1d58f7932b6ae8027c4e1d84f440ffe?s=128&d=identicon&r=PG"));
        dialog.setIconImage( image );
        dialog.setVisible(true);
    }
}
Andrew Thompson
trashgod
I only tested it on Windows. Not sure if it works on Linux.
Andrew Thompson
A: 

HI,

This would not work as I have some pre selected value that I need to populate the Input dialog box with and hence cant use the JOptionPane constructor but instead have to use showInputDialog method..

Hence, I believe I cannot use a custome icon when using showInputDialog(.,.,.,.,.,.,.)

Thanks.

Global Dictator
A: 

The JOptionPane takes its icon from the parent frame. So you can set the icon on a dummy JFrame, and pass that into the JOptionPane call:

    BufferedImage image = ImageIO.read(new URL(
    "http://www.gravatar.com/avatar/f1d58f7932b6ae8027c4e1d84f440ffe?s=128&d=identicon&r=PG"));
    JFrame frame = new JFrame();
    frame.setIconImage(image);
    JOptionPane.showInputDialog(frame, "Enter Address", "Chat",
            JOptionPane.QUESTION_MESSAGE, null, null, "");

Note, this will likely cause issues with the location of the shown dialog, as it will be placed relative to the dummy JFrame passed in.

wolfcastle