Get the icon used in the JOptionPane to signify its message type. - Java Swing

Java examples for Swing:Icon

Description

Get the icon used in the JOptionPane to signify its message type.

Demo Code

/*/*from  w w  w .j a  v a2s.co m*/
 * Copyright (c) 2007 BUSINESS OBJECTS SOFTWARE LIMITED
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *  
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *  
 *     * Neither the name of Business Objects nor the names of its contributors
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 *  
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
//package com.java2s;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.util.HashMap;
import java.util.Map;

import javax.swing.Icon;

import javax.swing.JOptionPane;

import javax.swing.plaf.OptionPaneUI;

import javax.swing.plaf.basic.BasicOptionPaneUI;

public class Main {
    /**
     * Used by getJOptionPaneIcon().
     * Cache from message type (JOptionPane int constant representing message type)
     * to icon representing that message type.
     */
    private static final Map<Integer, Icon> jOptionPaneMessageTypeToIconMap = new HashMap<Integer, Icon>();

    /**
     * Get the icon used in the JOptionPane to signify its message type.
     * Note that this is almost the same as a call to something like: UIManager.getIcon("OptionPane.questionIcon")
     *  but that doesn't work on GTK look-and-feel since there is no corresponding entry in its laf defaults map.
     *  
     * @param jOptionPaneMessageType One of the message types constants defined in the javax.swing.JOptionPane class.
     * @return the corresponding icon, or null if the icon is undefined for the provided message type.
     */
    public static Icon getJOptionPaneIcon(int jOptionPaneMessageType) {
        /*
         * HACK - workaround for GTK.
         * We create a new JOptionPane, get its UI (which should be a BasicOptionPaneUI), then call BasicOptionPaneUI.getIconForType().
         * GTK uses SynthOptionPaneUI (which is package protected), which eventually defers to SynthDefaultLookup to look
         * up default values for ui items such as icons.
         * However using this with non-Synth UIs causes lookup to defer to the basic (non-synth) lookup scheme.
         * Since we can't instantiate SynthOptionPaneUI (which is the SynthUI that we want) we are stuck calling the protected
         * method getIconForType() reflectively after unprotecting it.
         */

        Integer messageTypeInteger = new Integer(jOptionPaneMessageType);

        Icon icon = jOptionPaneMessageTypeToIconMap.get(messageTypeInteger);

        if (icon == null) {

            // Get the icon from a new JOptionPane's UI.
            OptionPaneUI ui = (new JOptionPane()).getUI();

            // The code below only works for BasicOptionPaneUIs.
            if (!(ui instanceof BasicOptionPaneUI)) {
                ui = new BasicOptionPaneUI();

                // Have to use a UI with a JOptionPane for it to initialize its icons.
                JOptionPane optionPane = new JOptionPane();
                optionPane.setUI(ui);
            }

            BasicOptionPaneUI optionPaneUI = (BasicOptionPaneUI) ui;

            try {
                Method method = BasicOptionPaneUI.class.getDeclaredMethod(
                        "getIconForType", new Class[] { int.class }); //$NON-NLS-1$
                boolean oldAccessible = method.isAccessible();
                method.setAccessible(true);
                try {
                    icon = (Icon) method.invoke(optionPaneUI,
                            new Object[] { new Integer(
                                    jOptionPaneMessageType) });
                } finally {
                    method.setAccessible(oldAccessible);
                }

            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }

            jOptionPaneMessageTypeToIconMap.put(messageTypeInteger, icon);
        }

        return icon;
    }
}

Related Tutorials