Input Validator : FocusLost event handling
I am new to working with FocusListener and I am trying to use it validate the input of a textfield to be an integer.
This seems like a seemingly simple thing to do, but I am getting errors.
Here is a portion of the code where I make the textfield and assign it to a focus listener.
InputValidator i =new InputValidator();
...
resolutionlabel =new JLabel("Resolution: ");
p.add(resolutionlabel);
resolutiontextbox =new JTextField("3200");
resolutionlabel.setLabelFor(resolutiontextbox);
resolutiontextbox.addFocusListener(i);
p.add(resolutiontextbox);
Here is the important parts of the focus listener code:
publicclass InputValidatorextends FocusAdapter
{
public InputValidator()
{
}
publicvoid focusLost(FocusEvent event)
{
validate((TextField)(event.getSource()));
}
...
privatevoid validate(TextField field)
{
...
}
}
The error I am getting is on the line where I call the validate function and is listed as :
java.lang.ClassCastException: javax.swing.JTextField

