Java JOptionPane Message getJOptionPaneIcon(int jOptionPaneMessageType)

Here you can find the source of getJOptionPaneIcon(int jOptionPaneMessageType)

Description

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

License

Open Source License

Parameter

Parameter Description
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.

Declaration

public static Icon getJOptionPaneIcon(int jOptionPaneMessageType) 

Method Source Code

//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 {
    /**//w ww  .ja  va  2s .c o m
     * 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", //$NON-NLS-1$
                        new Class[] { int.class });
                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

  1. getIconForType(int messageType)
  2. getInput(String msg, String defaultInput)
  3. getInt(String message, int min, int max, Integer initialValue)
  4. getIntegerFromImput(String message, int min, int max)
  5. getIntegerInput(String message)
  6. getPassword(String message)
  7. getString(Component component, String message)
  8. getStringInput(String message)
  9. getStrInPane(String message, String title)