focusLost event

Posted on 2004-11-02
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
    LVL 2

    Expert Comment

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

    Accepted Solution

    Hope this model might help you

    import javax.swing.*;
    import java.awt.event.*;
     * 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


    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    java ^ examples 8 40
    bigHeights  challenge 13 28
    count8 challlenge 13 34
    How Complex Is This Java Course ? 9 32
    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    933 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now