File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes focus problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "focus problem" Watch "focus problem" New topic

focus problem

selvas kumars
Ranch Hand

Joined: Jan 06, 2001
Posts: 115
Hi folks,
when window gets focus i want fire my method where both gained focus and lost focus are not getting executed..
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.Vector;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
public class FocusEventDemo extends JFrame {
public FocusEventDemo(){
addFocusListener(new fa(){

public void focusGained(FocusEvent e) {
System.out.println("focus gained fired");
public void focusLost(FocusEvent e) {
System.out.println("focus gained fired");
public static void main(String args[]){
FocusEventDemo demo = new FocusEventDemo();
class fa extends FocusAdapter{

Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Selvas,
If you are interested in windows then you should be using WindowListener --> windowActivated, windowDeactivated
For windows that should be equivalent for focus gained and lost on any other component.
I agree. Here's the link:
subject: focus problem
It's not a secret anymore!