?
Solved

focusLost event

Posted on 2004-11-02
3
Medium Priority
?
309 Views
Last Modified: 2012-06-27
Hi,
  I have a JTextField and when focusLost event is triggered it get details from the database and fill the other fields.
So there is a check there to make sure the JTextField is not empty to do so.  That is grand but if focus is on this JTextField and I hit the canel button I get the message the JTextField cannot be empty.

Is there anyway to find out in the FocusLost if the next component to get the focus is a Cancel button?

Or is there another way to do this.

thanks
0
Comment
Question by:Bourke
3 Comments
 
LVL 2

Expert Comment

by:tdisessa
ID: 12474843
Should the Cancel button be checking if any of the fields are empty if you are just cancelling the form?
0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 150 total points
ID: 12474998
Hope this model might help you

import javax.swing.*;
import java.awt.event.*;
/*
 * MyTextFieldText.java
 *
 * Created on November 2, 2004, 10:49 PM
 */

/**
 *
 * @author  Sudhakar
 */
public class MyTextFieldText extends javax.swing.JFrame {
   
    JTextField field=new JTextField("field");
    JTextField field1=new JTextField("field1");
    JTextField field2=new JTextField("field2");
    JButton b=new JButton("Cancel");
    public MyTextFieldText() {
       
       MyFocusListener listener= new MyFocusListener();
        field1.addFocusListener(listener);
        field2.addFocusListener(listener);
        b.addFocusListener(listener);
    }
   
    public static void main(String args[]) {
        new MyTextFieldText().show();
    }
   
  class MyFocusListener extends FocusAdapter {
        public void focusGained(FocusEvent evt) {
        if(evt.getSource()!=b)
        {
           if(field1.getText().trim().length()==0)
           {
               field1.requestFocus();
               return;
           }
        }                
        }        
    }
  }
0
 

Author Comment

by:Bourke
ID: 12481126
sudhakar

The code you gave me above is what I have, but it made me re-look at my own code and found I should not have had a Dialog where it should not be.

Thanks for your help
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month6 days, 7 hours left to enroll

594 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question