• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Focus Lost Problem for JTextField

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;


}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic