Source Code : Validate a value on the lostFocus event

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Validate a value on the lostFocus event

     

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Main {
  JTextField tf1;
  public void init() {
    tf1 = new JTextField(5);
 
    tf1.addFocusListener(new FocusListener() {
      public void focusGained(FocusEvent e) {
      };
      public void focusLost(FocusEvent e) {
        if (!e.isTemporary()) {
          String content = tf1.getText();
          if (!content.equals("a") ) {
            System.out.println("illegal value! " + content);
            SwingUtilities.invokeLater(new FocusGrabber(tf1));
          }
        }
      }
    });
  }
}

class FocusGrabber implements Runnable {
  private JComponent component;

  public FocusGrabber(JComponent component) {
    this.component = component;
  }

  public void run() {
    component.grabFocus();
  }
}

   
    
    
    
    
  

Thank with us