开发者

JOptionPane: Installing Custom icon has no effect when look and feel set

I would like to install a custom icon in place of the standard JOptionPane information icon..

I tried

ImageIcon myCustomIcon = ...;
UIManager.put("OptionPane.informationIcon", myCustomIcon);

However, apparently this has no effect due to the following line:

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Commenting out this line gives the correct behaviour. I have of 开发者_开发问答course tried putting the icon into the UIManager both before/after the look and feel is being set. Can I somehow combine the two to override the information icon globally?

I am working on Ubuntu 10.04 right now..

Regards, Morten


Works fine for me using the Metal and Windows LAF's.

Maybe your LAF doesn't support the UIManager property. Check out UIManager Defaults for a list of properties.

If you need more help the post your SSCCE that demonstrates the problem.


Have you tried to specify your icon after setting look&feel, in such a way:

JOptionPane.showMessageDialog(frame,
    "Eggs are not supposed to be green.",
    "Inane custom dialog",
    JOptionPane.INFORMATION_MESSAGE,
    myCustomIcon);

Update:

The following code works fine on my Windows 7:

import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class OptionPaneIcon {
    public static void main (String[] args) {
        ImageIcon myCustomIcon = loadImageIcon("image.png");
        UIManager.put("OptionPane.informationIcon", myCustomIcon);
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
        } catch (InstantiationException ex) {
        } catch (IllegalAccessException ex) {
        } catch (UnsupportedLookAndFeelException ex) {
        }
        JOptionPane.showMessageDialog(null, "Hello!");
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    private static ImageIcon loadImageIcon(String path) {
        URL imgURL = OptionPaneIcon.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
                return null;
        }
    }
}

P.S. Sorry for my impatience.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜