• 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

 
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.
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;

}
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the need of separate thread to run this?
Can�t you have simple main method without thread?
Run it without thread and see.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a known problem in Java 1.4.x and probably other versions as well. Java generates an extra focus lost event on the first field if you go from one field to the next one and force focus back to the first one. The event happens when the second field looses focus.
My trick is to set the background of the first field to red if you detect an "error" in a field, modify the focus lost code to no action if the background is red. That will filter out the second focus lost event. Set the color back to normal when you start typing. I use the keyboard event for this.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic