This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Validating empty JTextField Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Validating empty JTextField Problem" Watch "Validating empty JTextField Problem" New topic
Author

Validating empty JTextField Problem

satya sahu
Ranch Hand

Joined: Mar 26, 2003
Posts: 97
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.
I am Attaching the code below. Can somebody correct this code.
Thanks


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


public class ValidationTest extends javax.swing.JDialog {


public ValidationTest(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
this.setSize(400, 300);
}


private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();

getContentPane().setLayout(null);

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusLost(java.awt.event.FocusEvent evt) {
jTextField1FocusLost(evt);
}
});

getContentPane().add(jTextField1);
jTextField1.setBounds(120, 70, 160, 20);

getContentPane().add(jTextField2);
jTextField2.setBounds(120, 110, 160, 20);

getContentPane().add(jTextField3);
jTextField3.setBounds(120, 150, 160, 20);

pack();
}


protected 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,
"Please enter some value",
"Error", JOptionPane.OK_OPTION);
textField.requestFocus();
evt.consume();
}

if (content.length() != 0)
{
boolean valid_value = false;
try
{
Integer.parseInt(content);
valid_value = true;
}
catch (NumberFormatException nfe)
{
getToolkit().beep();
JOptionPane.showMessageDialog(this,
"Numbers only please .... (0-9)",
"Error", JOptionPane.OK_OPTION);

textField.setText("");
textField.requestFocus();
}
if(valid_value)
{
valid_value = false;
jTextField3.requestFocus();
}
}
}

public static void main(String args[]) {

ValidationTest obj = new ValidationTest(new javax.swing.JFrame(), true);
obj.setVisible(true);

}

private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;


}
 
GeeCON Prague 2014
 
subject: Validating empty JTextField Problem