Global Dictator
Global Dictator

Reputation: 1589

Using custom icons in title with JOptionPane.showInputDialog

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

Upvotes: 1

Views: 12513

Answers (4)

wolfcastle
wolfcastle

Reputation: 5930

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.

Upvotes: 3

Global Dictator
Global Dictator

Reputation: 1589

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.

Upvotes: 0

Andrew Thompson
Andrew Thompson

Reputation: 168825

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);
    }
}

Upvotes: 4

Qwerky
Qwerky

Reputation: 18445

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.

Upvotes: 1

Related Questions