Java JButton Settings showOptions(int width, int type, String title, String text, Object... buttons)

Here you can find the source of showOptions(int width, int type, String title, String text, Object... buttons)

Description

Shows a message with options presented as an array of buttons.

License

Open Source License

Parameter

Parameter Description
width Integer value, in pixels.
type Type of message.
title Title of the message.
text Text of the message.
buttons An array of objects to be presented as buttons.

Return

The index of the selected button, starting from 1.

Declaration

public static int showOptions(int width, int type, String title, String text, Object... buttons) 

Method Source Code


//package com.java2s;
import javax.swing.JOptionPane;

public class Main {
    private static final int WIDTH = 250;

    /**//  w  ww. j  a va2 s . com
     * Shows a message with options presented as an array of buttons.
     * @param width Integer value, in pixels.
     * @param type Type of message.
     * @param title Title of the message.
     * @param text Text of the message.
     * @param buttons An array of objects to be presented as buttons.
     * @return The index of the selected button, starting from 1.
     */
    public static int showOptions(int width, int type, String title, String text, Object... buttons) {

        // returns the index of the selected button,
        // zero if nothing is selected
        return JOptionPane.showOptionDialog(null,
                String.format("<html><body style=\"width:%dpx\">%s</body></html>", normalizeMessageWidth(width),
                        text),
                title, JOptionPane.DEFAULT_OPTION, normalizeIconType(type), null, buttons, buttons[0]) + 1;
    }

    /**
     * Shows a message with options presented as an array of buttons. It relies
     * on the default width.
     * @param type Type of message.
     * @param title Title of the message.
     * @param text Text of the message.
     * @param buttons An array of objects to be presented as buttons.
     * @return  The index of the selected button, starting from 1.
     */
    public static int showOptions(int type, String title, String text, Object... buttons) {
        return showOptions(WIDTH, type, title, text, buttons);
    }

    /**
     * Normalizes the message width, so only valid nonzero values are accepted.
     * @param value An integer value corresponding to the message width.
     * @return The normalized width.
     */
    private static int normalizeMessageWidth(int value) {
        return (value > 0 ? value : WIDTH);
    }

    /**
     * Normalizes the icon type to one of the five available icons.
     * @param value An integer value.
     * @return The normalized integer value.
     */
    private static int normalizeIconType(int value) {

        // do the normalization according to the available
        // icons in the underlying message implementation
        switch (value) {
        case 1:
            value = JOptionPane.ERROR_MESSAGE;
            break;
        case 2:
            value = JOptionPane.INFORMATION_MESSAGE;
            break;
        case 3:
            value = JOptionPane.WARNING_MESSAGE;
            break;
        case 4:
            value = JOptionPane.QUESTION_MESSAGE;
            break;
        default:
            value = JOptionPane.PLAIN_MESSAGE;
            break;
        }
        return value;
    }
}

Related

  1. setToggleGroups(final JToggleButton... buttons)
  2. setToggling(AbstractButton b)
  3. setWithoutNotifyingListeners(AbstractButton button, boolean selected)
  4. showJFileChooser(javax.swing.JFileChooser chooser, java.awt.Component parent, java.lang.String approveButtonText)
  5. showJFileChooser(JFileChooser chooser, Component parent, String approveButtonText)
  6. styleButton(final AbstractButton btn)
  7. toBold(final T button)
  8. tryToBuildAFocusGroup(AbstractButton... buttons)
  9. unifiedButtonLookAndFeel(JComponent b)