Hi
I have two JTextFields and FocusListener is added to both the field. When the first field loses focus, I am checking the contents of the first field.
If the text contains other than "Country", then the focus is set back to the first field.
I am getting the correct functionality but the focus is set to the second field and then it is set to first field.
What can be done to stop the second field getting focus in case to wrong entry?
I have also pasted the code
Thanks. Any help is appreciated.
//All classes are imported
public class NewClass implements FocusListener{
JFrame frame;
JTextField userNameField;
JTextField passwordField;
JPanel mainPanel;
public NewClass() {
mainPanel = new JPanel();
userNameField = new JTextField(10);
userNameField.addFocusListener(this);
userNameField.setName("UserNameField");
passwordField = new JTextField(10);
passwordField.addFocusListener(this);
passwordField.setName("PasswordField");
mainPanel.add(userNameField);
mainPanel.add(passwordField);
frame = new JFrame("TableExample");
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setVisible(true);
frame.setBounds(200, 200, 300, 300);
}
public static void main(
String s[]) {
new NewClass();
}
public void focusGained(FocusEvent e) {
System.out.println(((JTextField)e.getSource()).getName());
}
public void focusLost(FocusEvent e) {
String text = ((JTextField)e.getSource()).getText();
if(!(text.equalsIgnoreCase(("Country"))))
userNameField.requestFocus(true);
}
}