my dog learned polymorphism*
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Similar Threads
focussing swing buttons using tab key
ask some questions about FocusListener
JComboBox and FocusListener
FocusListner not working on JMenu
In the swing of JFrame.setFocusableWindowState (false),Corresponds to what is in the method in swt