简体   繁体   中英

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

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM