
May 8th, 2008, 09:08 PM
|
|
Registered User
|
|
Join Date: May 2008
Posts: 9

Time spent in forums: 1 h 27 m 19 sec
Reputation Power: 0
|
|
|
Homework - Help with formatting a TextField.
This is part of the code of a program we are working on in our Java class. The assignment is done but a classmate and I are just trying to work on one final graphical issue that results when the user enters a valid "Social Security Number"(just a 9 digit number) after previously entering an invalid one.
Code:
// private inner class for event handling
private class TextFieldHandler implements ActionListener
{
// process textfield events
public void actionPerformed ( ActionEvent event )
{
String socialInput = socialTextField.getText (); // declare string to display
socialTextField.selectAll ();
socialTextField.requestFocus ();
socialTextField.setText ( "" );
socialLabelPrompt.setText ( "Please enter a Social Security Number" );
try
{
arrayStorage.validateSSN (socialInput);
arrayStorage.addSocial (socialInput);
System.out.printf ( "\n%8s", "Social Security Number: " );
System.out.println (socialInput);
}
catch (InputMismatchException e)
{
socialLabelPrompt.setText ( e.getMessage () + ". " + "Please reenter Social Security Number" );
socialTextField.setText ( "SOCIAL SECURITY NUMBER" );
socialTextField.selectAll ();
socialTextField.requestFocus ();
socialLabelPrompt.setHorizontalTextPosition ( SwingConstants.CENTER );
socialLabelPrompt.setVerticalTextPosition ( SwingConstants.BOTTOM );
}
catch (DuplicateName duplicateSocialError)
{
}
}
}
More specifically the area of code, I believe, is:
Code:
String socialInput = socialTextField.getText (); // declare string to display
socialTextField.selectAll ();
socialTextField.requestFocus ();
socialTextField.setText ( "" );
socialLabelPrompt.setText ( "Please enter a Social Security Number" );
socialTextField.setText( "" ); is the line in question. Is there a way to make that always wide enough for 9 characters?
Putting \t in the quotes puts a tab, but the user needs to backspace to remove the tab; this is not desirable. Is there any modification I can make that would allow me to keep the field empty and still have it at a specific width?
Thanks.
|