Hi All,
I have few text fields. I have to do the validations for these text fields. If wrong input is given then some message should be displayed. If no value is entered and Tab is pressed then also some message box should be displayed asking the user to enter some values. I am giving the code that i was trying.
I have problem with when no values are entered in the text field. The message box is poped more than once.
Can somebody correct this code.
Thanks
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class NewJDialog extends javax.swing.JDialog {
/** Creates new form NewJDialog */
public NewJDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
this.setSize(500, 400);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jPanel1 = new javax.swing.JPanel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
getContentPane().setLayout(new java.awt.GridBagLayout());
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jPanel1.setLayout(null);
jPanel1.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
jPanel1FocusLost(evt);
}
});
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
jTextField1FocusLost(evt);
}
});
jPanel1.add(jTextField1);
jTextField1.setBounds(120, 40, 220, 40);
jPanel1.add(jTextField2);
jTextField2.setBounds(120, 100, 220, 40);
jPanel1.add(jTextField3);
jTextField3.setBounds(120, 160, 220, 40);
jPanel1.add(jTextField4);
jTextField4.setBounds(120, 220, 220, 40);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(jPanel1, gridBagConstraints);
pack();
}
// </editor-fold>
private void jTextField1FocusLost(java.awt.event.FocusEvent evt) {
JTextField textField = (JTextField)evt.getSource();
String content = textField.getText();
System.out.println("current length = "+content.length());
System.out.println("content = "+content);
if (content.trim().length() == 0)
{
JOptionPane.showMessageDialog(this,
"Pleae enter some Value",
"Error", JOptionPane.OK_OPTION);
textField.requestFocus();
}
if (content.length() != 0)
{
try
{
Integer.parseInt(content);
}
catch (NumberFormatException nfe)
{
getToolkit().beep();
JOptionPane.showMessageDialog(this,
"Numbers only please .... (0-9)",
"Error", JOptionPane.OK_OPTION);
textField.setText("");
textField.requestFocus();
}
}
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void jPanel1FocusLost(java.awt.event.FocusEvent evt) {
JTextField textField = (JTextField)evt.getSource();
String content = textField.getText();
System.out.println("current length = "+content.length());
System.out.println("content = "+content);
if (content.trim().length() == 0)
{
Object[] options = { "OK" };
int value = JOptionPane.showOptionDialog(this, "Please enter some value", "Error",
JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
null, options, options[0]);
if(value == 0)
{
System.out.println("value = "+value);
textField.requestFocus();
return;
}
}
if (content.length() != 0)
{
try
{
Integer.parseInt(content);
}
catch (NumberFormatException nfe)
{
getToolkit().beep();
JOptionPane.showMessageDialog(this,
"Numbers only please .... (0-9)",
"Error", JOptionPane.OK_OPTION);
textField.setText("");
textField.requestFocus();
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJDialog(new javax.swing.JFrame(), true).setVisible(true);
}
});
}
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
}