Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes Focus Lost Problem for JTextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Focus Lost Problem for JTextField" Watch "Focus Lost Problem for JTextField" New topic
Author

Focus Lost Problem for JTextField

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;


}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Focus Lost Problem for JTextField
 
Similar Threads
Validating empty JTextField Problem
Problem with Email Program in TCP
simple login - help asp
how to apply actionperformed to Jtextfields in java ?
Focus Lost problem