Java JOptionPane Message chooseIndex(String message, String messageTitle, Object[] objects, Object initialObject)

Here you can find the source of chooseIndex(String message, String messageTitle, Object[] objects, Object initialObject)

Description

Asks the User to select an Object from a given array of Objects.

License

Open Source License

Parameter

Parameter Description
message a parameter
messageTitle a parameter
objects a parameter
initialObject a parameter

Exception

Parameter Description
Exception an exception

Return

the index of the chosen object

Declaration

public static int chooseIndex(String message, String messageTitle, Object[] objects, Object initialObject)
        throws Exception 

Method Source Code

//package com.java2s;
/*// w  w w .  j a va2s. c  om
 * Copyright (C) 2010-2014  Andreas Maier
 * CONRAD is developed as an Open Source project under the GNU General Public License (GPL).
 */

import javax.swing.JOptionPane;

public class Main {
    /**
     * Asks the User to select an Object from a given array of Objects.
     * The index of the selected object is returned.
     * @param message
     * @param messageTitle
     * @param objects
     * @param initialObject
     * @return the index of the chosen object
     * @throws Exception
     */
    public static int chooseIndex(String message, String messageTitle, Object[] objects, Object initialObject)
            throws Exception {
        Object input = JOptionPane.showInputDialog(null, message, messageTitle, JOptionPane.INFORMATION_MESSAGE,
                null, objects, initialObject);
        if (input == null)
            throw new Exception("Selection aborted");
        int index = -1;
        for (int i = 0; i < objects.length; i++) {
            if (objects[i].equals(input))
                index = i;
        }
        return index;
    }
}

Related

  1. alert(Component component, String title, String msg)
  2. askPassword(String message)
  3. browse(String url, Component msgParent)
  4. browse(String url, Component msgParent)
  5. chooseObject(String message, String messageTitle, Object[] objects, Object initialObject)
  6. copyToClipBoard(String code, boolean displayMessage)
  7. createPane(Component comp, String msg)
  8. createPane(int type, String message, int messageMaxLength)