java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow javax.swing arrow Validate a value on the lostFocus event
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4090
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Validate a value on the lostFocus event E-mail
User Rating: / 6
PoorBest 

The problem is when the lostFocus occurs on a Component, the gainedFocus is already sent for the next component in the SystemEventQueue. We must grab this event, and request the focus for the previous component (if there is a validation error). We can't use Toolkit.getDefaultToolkit().getSystemEventQueue() directly to remove the gainedFocus event because of security restriction in Applet. This can be done with invokeLater method of the SwingUtilities class.

 import java.awt.*; 
 import java.awt.event.*;
 import javax.swing.*;

 public class tswing extends JApplet {

   JTextField textfield1, textfield2;
   JLabel label1;

   public void init() {

     getContentPane().setLayout(new FlowLayout());

     //
     label1 = new JLabel("must be 'a' or 'b' ");
     textfield1 = new JTextField(5);
     getContentPane().add(label1);
     getContentPane().add(textfield1);
     textfield2 = new JTextField(5);
     getContentPane().add(textfield2);

     textfield1.addFocusListener(new FocusListener() {
        public void focusGained(FocusEvent e) {}// not needed

        public void focusLost(FocusEvent e) {

          if (!e.isTemporary() && isEnabled() ) {
            String fieldContent = textfield1.getText();
            if (!fieldContent.equals("a"&& !fieldContent.equals("b")) {
              Toolkit.getDefaultToolkit().beep();
              System.out.println("illegal value! " + fieldContent );
              SwingUtilities.invokeLater(new FocusGrabber(textfield1));         
              }
          }}
        });
     }
 }


import javax.swing.*;

public class FocusGrabber implements Runnable {
 
  private JComponent component;
 
  public FocusGrabber(JComponent component) {
   this.component = component;
  }
  
  public void run() {
   component.grabFocus();
  }
}

JDK1.3 provides a new class, InputVerfier, which can be used to do that.

 import java.awt.*;
 import java.util.*;
 import java.awt.event.*;
 import javax.swing.*;

 // the first JTextField expects the string "howto" as input, 
 // and will allow focus to change only if the required string
 // is typed.

 class VerifierTest extends JFrame {
  public VerifierTest () {
    JTextField tf,tf2;
    tf = new JTextField ("howto is required");

    getContentPane().add (tf, BorderLayout.NORTH);
    tf.setInputVerifier(new HowtoVerifier());

    tf2 = new JTextField ("howto come here");
    getContentPane().add (tf2, BorderLayout.SOUTH);

    addWindowListener(new WindowCloser());
  }
 

 public static void main (String [] args) {
   Frame f = new VerifierTest ();
   f.pack();
   f.show();
 }
}

class WindowCloser extends WindowAdapter {
 public void windowClosing(WindowEvent e) {
   Window win = e.getWindow();
   win.setVisible(false);
   System.exit(0);
 }
}


class HowtoVerifier extends InputVerifier {
  public boolean verify(JComponent input) {
    JTextField tf = (JTextFieldinput;
    String pass = tf.getText();
    return pass.equals("howto");
  }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.