3

I'm creating an application with Swing, and I've got a problem. I want to handle the focusLost() and focusGained() events, but I think my code is buggy.

I've got the following, test code:

addWindowFocusListener(new WindowFocusListener() {
    @Override
    public void windowLostFocus(WindowEvent e) {
        System.out.println("Lost!");
        System.out.println(e.toString());
    }

    @Override
    public void windowGainedFocus(WindowEvent e) {
        System.out.println("Gained!");
        System.out.println(e.toString());
    }
});

And, when I activate the window, it seems that app handles 2 events at once:

Gained!
java.awt.event.WindowEvent[WINDOW_GAINED_FOCUS,opposite=null,oldState=0,newState=0] on frame0
Lost!
java.awt.event.WindowEvent[WINDOW_LOST_FOCUS,opposite=null,oldState=0,newState=0] on frame0

And, when I deactivate the window, the program prints nothing.

There's also more interesting thing. When I open a new JFrame that belongs to my app, the event system works properly, even when I close this second Window.

I just don't know why it is happening. Please help.

System specs:

  • OS: Ubuntu 12.04 x86_64
  • JVM: OpenJDK 7
m4tx@m4tx-EP35-DS4:~$ java -version
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=lcd
java version "1.7.0_03"
OpenJDK Runtime Environment (IcedTea7 2.1.1pre) (7~u3-2.1.1~pre1-1ubuntu3)
OpenJDK 64-Bit Server VM (build 22.0-b10, mixed mode)
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
m4tx
  • 4,139
  • 5
  • 37
  • 61
  • 2
    Can you post a [Short, Self Contained, Correct Example (SSCCE)](http://www.sscce.org/) that demonstrates your problem (i.e. something that can be run as is)? – assylias Jul 23 '12 at 17:09
  • Please post the code. I undone the down vote by giving you an up vote, because of my policy about questions. – PeakGen Jul 23 '12 at 17:44
  • At this moment I can't... I wrote simple example for myself, but there's everything ok. An app, where I have this problem is a bit big app, so I can't upload the code there. I think the problem may be caused by jMonkeyEngine (lwjgl) canvas. If I would be able to write SSCCE, I would write it... – m4tx Jul 23 '12 at 18:07
  • Yeah, I deleted the jME canvas and it works ok. I'll write a example code as fast as I can. – m4tx Jul 23 '12 at 18:10
  • It sounds like you're mixing AWT and Swing. – trashgod Jul 23 '12 at 18:27
  • It's possible that you have the same listener attached twice – MadProgrammer Jul 23 '12 at 19:29
  • No. Notice that there're 2 events (gained and lost), when window gains focus, and no events when window losts focus. – m4tx Jul 24 '12 at 05:58

1 Answers1

4

Well, I can't see much wrong with your code; so I put together a quick demo, and it seems to be working fine:

import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class WindowEventDemo extends JFrame implements WindowFocusListener {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new WindowEventDemo("Window Event Demo").setVisible(true);
            }
        });
    }

    public WindowEventDemo(String name) {
        super(name);
        addWindowFocusListener(this);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 300);
    }

    @Override
    public void windowGainedFocus(WindowEvent e) {
        System.out.println("WindowFocusListener method called: windowGainedFocus.");
    }

    @Override
    public void windowLostFocus(WindowEvent e) {
        System.out.println("WindowFocusListener method called: windowLostFocus.");
    }
}

On start up it prints:

WindowFocusListener method called: windowGainedFocus.

and when the window is minimized, or the mouse is clicked somewhere else on screen besides the JFrame is prints:

WindowFocusListener method called: windowLostFocus.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138