/Users/petercappello/NetBeansProjects/56-2014/util/src/IntegerOptionPane.java

import javax.swing.JOptionPane;

/*
 * Option pane that accepts only strings that have an interpretation as an int.
 */

/**
 *
 * @author Peter Cappello
 */
public class IntegerOptionPane 
{
    private final String promptString;
    
    /**
     * Constructs an IntegerOptionPane object that uses promptString as the 
     * option pane user prompt.
     * @param promptString prompt string used in option pane.
     */
    public IntegerOptionPane( String promptString ) { this.promptString = promptString; }
    
    /**
     * Get the value as an integer of an entered String.
     * @return value as an integer of entered String.
     */
    public int getIntValue()
    {
        int value;
        while ( true )
        {
            String enteredString = JOptionPane.showInputDialog( promptString );
            try
            {
                value = Integer.parseInt( enteredString );
                break;
            }
            catch ( NumberFormatException numberFormatException )
            {
                StringBuilder errorString = new StringBuilder();
                errorString.append( "`" ).append( enteredString );
                errorString.append( "' cannot be interpreted as an integer." );
                JOptionPane.showMessageDialog( null, errorString, "Number Format Exception" , JOptionPane.ERROR_MESSAGE );
            }
        }
        return value;
    }
    
    /**
     * construct a string version of the object.
     * @return string value: the prompt string.
     */
    @Override
    public String toString()
    {
        StringBuilder string = new StringBuilder();
        string.append( "promptString: " ).append( promptString );
        return new String( string );
    }
    
    /**
     * Unit test the IntegerOptionPane class.
     * @param args unused
     */
    public static void main( String[] args )
    {
        IntegerOptionPane optionPage = new IntegerOptionPane( "Enter a number.");
        System.out.println( optionPage );        
        System.out.println( optionPage.getIntValue() );
    }
}