/Users/petercappello/NetBeansProjects/56-2014/util/src/IntegerOptionPane.java |
import javax.swing.JOptionPane;
@author
public class IntegerOptionPane
{
private final String promptString;
Constructs an IntegerOptionPane object that uses promptString as the
option pane user prompt.
@param
public IntegerOptionPane( String promptString ) { this.promptString = promptString; }
Get the value as an integer of an entered String.
@return
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
@Override
public String toString()
{
StringBuilder string = new StringBuilder();
string.append( "promptString: " ).append( promptString );
return new String( string );
}
Unit test the IntegerOptionPane class.
@param
public static void main( String[] args )
{
IntegerOptionPane optionPage = new IntegerOptionPane( "Enter a number.");
System.out.println( optionPage );
System.out.println( optionPage.getIntValue() );
}
}