Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6874085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:09:03+00:00 2026-05-27T04:09:03+00:00

Is it possible to check if a jtextfield has been selected / de-selected (ie

  • 0

Is it possible to check if a jtextfield has been selected / de-selected (ie the text field has been clicked and the cursor is now inside the field)?

//EDIT
thanks to the help below here is a working example

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class test extends JFrame {

private static JPanel panel = new JPanel();
private static JTextField textField = new JTextField(20);
private static JTextField textField2 = new JTextField(20);

public test() {
    panel.add(textField);
    panel.add(textField2);
    this.add(panel);
}

public static void main(String args[]) {

    test frame = new test();
    frame.setVisible(true);
    frame.setSize(500, 300);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    textField.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            System.out.println("selected");
        }

        @Override
        public void focusLost(FocusEvent e) {
            System.out.println("de-selected");
        }
    });
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:09:04+00:00Added an answer on May 27, 2026 at 4:09 am

    You will need to use the focusGained and focusLost events to see when it has been selected, and when it is deselected (i.e. gained/lost focus).

    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    
    import javax.swing.JTextField;
    
    public class Main {
    
        public static void main(String args[]) {
            final JTextField textField = new JTextField();
            textField.addFocusListener(new FocusListener() {
    
                @Override
                public void focusGained(FocusEvent e) {
                    //Your code here
                }
    
                @Override
                public void focusLost(FocusEvent e) {
                    //Your code here
                }
            });
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to check if a dynamically loaded assembly has been signed with
Is it possible to check if a char has been passed into a method
Is it possible to check whether a browser-provided function has been replaced by user
I've a text field which extends javax.swing.JTextField and also have document listener like this
Is it possible to check if all select values inside a div , inside
Possible Duplicates: Check if text is in a string JavaScript: string contains I'm trying
Is it possible to check if a TextCtrl is under keyboard focus (blinking cursor
Is it possible to check if the client browser has javascript enabled from ASP.NET
I am trying to check that the text in a JTextField matches a perticular
It possible to check if the cursor is hovering on an element. Something like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.