InputVerifer unable to exit with cancel button.
Just playing with the verifier and notice that I am unable to "cancel" when I failed the validation. I understand that I can not conintue to next field, but if I failed but how come I can't cancel out of the form?I could exit if use the [x] on the upper right side. which uses the WindowAdapter.
Any idea on how I can permit my cancel button to work under this situation also?
Here is my sample code. The basic code I got it from http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/InputVerifier.html
modified for my own test.
package verifier;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
publicclass VerifierTestextends JFrame{
public VerifierTest(){
JTextField tf1 =new JTextField ();
getContentPane().add (tf1, BorderLayout.NORTH);
tf1.setInputVerifier(new PassVerifier());
JTextField tf2 =new JTextField ("next field");
getContentPane().add (tf2, BorderLayout.CENTER);
JButton buttonCancel =new JButton("cancel");
buttonCancel.addActionListener(new CancelListener());
getContentPane().add(buttonCancel,BorderLayout.SOUTH);
WindowListener l =new WindowAdapter(){
publicvoid windowClosing(WindowEvent e){
System.out.println(" win closing");
dispose();
}
};
addWindowListener(l);
}
class PassVerifierextends InputVerifier{
publicboolean verify(JComponent input){
JTextField tf = (JTextField) input;
if(tf.getText().equals(""))
{
input.setInputVerifier(null);
JOptionPane.showMessageDialog(null,"Error, textfield empty");
input.setInputVerifier(this);
returnfalse;
}
returntrue;
}
}
protectedclass CancelListenerimplements ActionListener{
publicvoid actionPerformed(ActionEvent event){
System.out.println(" cacnelListener Action");
dispose();// Or System.exit(0) if you don't want to do
// something on close.
}
}
publicstaticvoid main(String[] args){
JFrame f =new VerifierTest();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}

