focusLost event

Posted on 2004-11-02
Medium Priority
Last Modified: 2012-06-27
  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.

Question by:Bourke

Expert Comment

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

Accepted Solution

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();
    public static void main(String args[]) {
        new MyTextFieldText().show();
  class MyFocusListener extends FocusAdapter {
        public void focusGained(FocusEvent evt) {

Author Comment

ID: 12481126

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

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