开发者

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 JOptionPan开发者_运维技巧e 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


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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜