3

I have 2 textfields in a JFrame and I want to validate the data in textfield1 when the focus gets lost from textfield1. So I have used FocusListener and used showMessageDialog() in the FocusLost() method and that then sets back the focus back to textfield1. It works fine when I click on any component inside the JFrame window other than textfield1,but when I click anywhere outside the JFrame window, the showMessageDialog() gets called two times and the focus goes to textfield2 whereas the focus should remain on textfield1.

    @Override
    public void focusGained(FocusEvent e) {}

    @Override
    public void focusLost(FocusEvent e) {
        boolean show = false;
        String theRegex = "[0-9]";
        Pattern checkRegex = Pattern.compile(theRegex);
        Matcher regexMatcher = checkRegex.matcher( MemberID );
        while ( !regexMatcher.find() && show==false){
            JOptionPane.showMessageDialog(null,"Please enter numbers","Validation Error",JOptionPane.ERROR_MESSAGE);
            MemberID_Text.requestFocusInWindow();
    MemberID_Text.selectAll();
            show = true;

        }

    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
singha
  • 3,351
  • 3
  • 13
  • 4
  • 4
    You should use a [`InputVerifier`](http://docs.oracle.com/javase/7/docs/api/javax/swing/InputVerifier.html) for this purpose, it's what it was designed for. Check out [Validating Input](http://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html#inputVerification) for an example. It sounds like you've attached the focus listener to the field more then once. – MadProgrammer Nov 13 '12 at 04:17
  • Given the use of the `show` attribute, it would more logically be titled `shown` (past tense) or `hide`. – Andrew Thompson Nov 13 '12 at 05:10
  • use DocumentFilter, most of Swing Listeners fired twice – mKorbel Nov 13 '12 at 07:16
  • Use a `JFormattedTextField` if you want a `JTextField` with validation – Robin Nov 13 '12 at 09:38
  • Just a hint to make code more clean. I would go with: !show instead of show==false. – lcguida Nov 13 '12 at 14:26

1 Answers1

0

you can do this to verify if a number is entered, and avoid regex all together

     class IntVerifier extends InputVerifier {

  @Override public boolean verify(JComponent input) {
      String text =((JTextField) input).getText();
      int n = 0; 

          try {
      n = Integer.parseInt(text); } 

      catch (NumberFormatException e) {
  return false; 
       }

  return true;
      }
      }

then use the input verifier on the text field

 IntVerifier intv = new IntVerifier();      
 myTextField = new JTextField();
 myTextField.setInputVerifier(intv);
Meesh
  • 508
  • 1
  • 5
  • 17
  • 1
    please read the api doc of verify (hint: it mentions _side-effects_ :-) – kleopatra Nov 13 '12 at 15:18
  • @kleopatra Checks whether the JComponent's input is valid. This method should have no side effects. It returns a boolean indicating the status of the argument's input. isn't that what we want? [link](http://stackoverflow.com/questions/1073909/side-effect-whats-this) – Meesh Nov 13 '12 at 20:08
  • next step: check if your implementation complies with that contract ... and why not :-) – kleopatra Nov 14 '12 at 08:55
  • @singha _do not_ use this particular implementation, it violates its contract! (simply return false instead of showing the optionPane will fix it, though) – kleopatra Nov 14 '12 at 17:08
  • @kleopatra thank u for the suggestion doing the show option pane causes a side effect – Meesh Nov 15 '12 at 13:06