event

Handle focus changes example

In this tutorial we are going to see how to handle focus changed in a Java Desktop Application. You might find this particularly useful if you have an application that contains many objects and you want to monitor the focus changes over these objects, if you always want to know the object that the user interacts with. You can use that kind of information when you want your application to behave differently in some aspects according to which window the user is working on.

Basically, to handle focus changes in a Java application, one should follows these steps:

  • Create a new FocusAdapter instance.
  • Override focusGained method in order to customize the handling of that event. Now every time an object gains focus, this method will fire up.
  • Override focusLost method to customize the handling of that event. Now every time an object looses focus, this method will fire up.
  • Use addFocusListener of a specific component in order to add to it the above FocusAdapter.

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

package com.javacodegeeks.snippets.desktop;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class FocusListener {

  public static void main(String[] args) {

// Create frame with specific title

Frame frame = new Frame("Example Frame");

// Create a component to add to the frame; in this case a text area with sample text

final TextArea textArea = new TextArea("Click button to check its focus status...");

// Create a component to add to the frame; in this case a button

Button button = new Button("Click Me!!");

// Add a focus listener to handle focus changes

button.addFocusListener(new FocusAdapter() {

    public void focusGained(FocusEvent evt) {

  textArea.setText(textArea.getText() + "nButton focus gained");

    }

    public void focusLost(FocusEvent evt) {

    textArea.setText(textArea.getText() + "nButton focus lost");

    }

});

// Add the components to the frame; by default, the frame has a border layout

frame.add(textArea, BorderLayout.NORTH);

frame.add(button, BorderLayout.SOUTH);

// Show the frame

int width = 300;

int height = 300;

frame.setSize(width, height);

frame.setVisible(true);

  }

}

 
This was an example on how to handle focus changes in a Java Desktop Application.

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
Inline Feedbacks
View all comments
Back to top button