Java JOptionPane Message getIntegerFromImput(String message, int min, int max)

Here you can find the source of getIntegerFromImput(String message, int min, int max)

Description

get Integer From Imput

License

Open Source License

Declaration

public static int getIntegerFromImput(String message, int min, int max) 

Method Source Code

//package com.java2s;
/**//from  w w w  .  ja va2 s.c o m
 * Classe InputUtil
 * 
 * <br><br>License:    GNU General Public License Version 3<br>
 * 
 * @author     Pier Paolo Ciarravano  
 * @version     Vers. 0.9 (11/01/2010) 
 */

import javax.swing.JOptionPane;

import javax.swing.JDialog;

import javax.swing.JOptionPane;

public class Main {
    public static int getIntegerFromImput(String message, int min, int max) {
        int num = 0;

        JOptionPane optPane = new JOptionPane(message + " - Insert integer value: [" + min + "," + max + "]",
                JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
        optPane.setWantsInput(true);
        //JDialog d = optPane.createDialog(null, "Input Value");
        //d.setVisible(true);

        boolean isOk = false;
        while (!isOk) {
            try {
                JDialog d = optPane.createDialog(null, "Input Value");
                d.setVisible(true);
                d.requestFocus();
                Object o = optPane.getInputValue();
                String strValue = "";
                if (o != null)
                    strValue = (String) o;

                num = Integer.parseInt(strValue);
                isOk = true;
                d.dispose();
            } catch (NumberFormatException nfe) {
                isOk = false;
                continue;
            }
            if ((num < min) || (num > max)) {
                isOk = false;
            }
        }

        System.out.println("Value for input (" + message + ") is: " + num);
        return num;
    }
}

Related

  1. excMsg(String msg, Throwable t)
  2. exibeMsg(String msg, int tipoMsg)
  3. getIconForType(int messageType)
  4. getInput(String msg, String defaultInput)
  5. getInt(String message, int min, int max, Integer initialValue)
  6. getIntegerInput(String message)
  7. getJOptionPaneIcon(int jOptionPaneMessageType)
  8. getPassword(String message)
  9. getString(Component component, String message)