event

FocusListener example

This is an example that discusses how to use FocusListener in Java. This is a very handy feature when you have several components and you want to monitor and handle the event when on of them gains or looses focus.

In short, all you have to do in order to work with a FocusListener is:

  • Create a new FocusListener
  • Override the methods that correspond to the events that you want to monitor about the component e.g focusGained, focusLost and customize as you wish the handling of the respective events. Now every time the monitored component gains or looses focus the corresponding method will be executed.
  • Use the addFocusListener method of the component you want to monitor, in order to add the FocusListener you’ve created.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class FocusListenerExample {

    public static void main(String args[]) {

  JFrame jFrame = new JFrame();

  Container cPan = jFrame.getContentPane();

  FocusListener focusListener = new FocusListener() {

@Override

public void focusGained(FocusEvent event) {

    printInfo(event);

}

@Override

public void focusLost(FocusEvent event) {

    printInfo(event);

}

private void printInfo(FocusEvent event) {

    System.out.println("Source  : " + getComponentName(event.getComponent()));

    System.out.println("Opposite : "

+ getComponentName(event.getOppositeComponent()));

    System.out.println("Temporary: " + event.isTemporary());

}

private String getComponentName(Component component) {

    return (component == null) ? null : component.getName();

}

  };

  JPanel jPanel = new JPanel();

  JLabel jLabel = new JLabel("Label 1: ");

  JTextField textField = new JTextField("Type Here", 15);

  textField.setName("First");

  textField.addFocusListener(focusListener);

  jLabel.setDisplayedMnemonic(KeyEvent.VK_1);

  jLabel.setLabelFor(textField);

  jPanel.add(jLabel);

  jPanel.add(textField);

  cPan.add(jPanel, BorderLayout.NORTH);

  jPanel = new JPanel();

  jLabel = new JLabel("Label 2: ");

  textField = new JTextField("14.0", 10);

  textField.setName("Second");

  textField.addFocusListener(focusListener);

  textField.setHorizontalAlignment(JTextField.RIGHT);

  jLabel.setDisplayedMnemonic(KeyEvent.VK_2);

  jLabel.setLabelFor(textField);

  jPanel.add(jLabel);

  jPanel.add(textField);

  cPan.add(jPanel, BorderLayout.SOUTH);

  jFrame.pack();

  jFrame.setVisible(true);
    }
}

 
This was an example on how to use FocusListener in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button