Here you can find the source of getIntegerFromImput(String message, int min, int max)
public static int getIntegerFromImput(String message, int min, int max)
//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; } }