Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't this listener detect window close events?

I'm trying to listen for events on a single Frame via WindowStateListener.

import java.awt.Frame;
import java.awt.Label;

import java.awt.event.WindowStateListener;
import java.awt.event.WindowEvent;

public class UserInterface implements WindowStateListener
{
    public static void main(final String[] arguments)
    {
        UserInterface userInterface = new UserInterface();
    }

    public UserInterface()
    {
        Frame frame = new Frame("Graphics Example");
        frame.addWindowStateListener(this);
        frame.add(new Label("Hello, world!");
        frame.pack();
        frame.setVisible(true);
    }

    public void windowStateChanged(WindowEvent event)
    {
        System.out.println(event.paramString();
    }
}

It's working fine for minimization events, but not close events. WINDOW_CLOSING is definitely a valid WindowEvent value, and it's definitely something that Frame can throw. So why isn't it being passed to windowStateChanged()?

like image 816
Maxpm Avatar asked Dec 27 '22 11:12

Maxpm


1 Answers

WindowStateListeners are not notified of the window closing events. They are only notified of changes to the window's state, such as iconified or de-iconified. If you want close events, implement WindowListener (or extend WindowAdapter). This tutorial explains it http://download.oracle.com/javase/tutorial/uiswing/events/windowlistener.html.

like image 89
Jeff Storey Avatar answered Jan 05 '23 04:01

Jeff Storey