Luca
Luca

Reputation: 1776

AWT window listener doesn't respond

I'm studying AWT and swing, and I'm beginning with some simple GUIs to get the hang of it.I wrote the following code,which works fine until I try to close the application and it won't exit.

import java.awt.*;
import java.awt.event.*;

public class AWTCounter extends Frame{
    private Button button;
    private Label label;
    private TextField txt;
    private int count=0;
    public AWTCounter(){
        super("AWT Counter");
        addWindowListener(new WindowListener(){

            @Override
            public void windowActivated(WindowEvent arg0) {}

            @Override
            public void windowClosed(WindowEvent arg0) {
                System.out.println("closing time!");
                System.exit(0);
            }

            @Override
            public void windowClosing(WindowEvent arg0) {}

            @Override
            public void windowDeactivated(WindowEvent arg0) {}

            @Override
            public void windowDeiconified(WindowEvent arg0) {}

            @Override
            public void windowIconified(WindowEvent arg0) {}

            @Override
            public void windowOpened(WindowEvent arg0) {}

        });
        label = new Label();
        add(label);
        label.setText("Counter");
        txt = new TextField();
        txt.setEditable(false);
        add(txt);
        button = new Button("count");
        add(button);
        setSize(400,100);
        setLayout(new FlowLayout());
        setVisible(true);
        button.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                ++count;
                txt.setText(Integer.toString(count));
            }
        });

    }
    public static void main(String [] args){
        Thread t =new Thread(new Runnable(){
            public void run(){
                new AWTCounter();
            }
        });
        t.start();
    }
}

I "registered" the WindowListener in the source component (the AWTCounter in this case, which is a Frame) and implemented the only method I was going to use, but it never respond... any idea as to why it behaves like that?

thanks a lot guys!

Upvotes: 0

Views: 125

Answers (2)

RishikeshD
RishikeshD

Reputation: 189

Try doing this:

@Override
public void windowClosing(WindowEvent event) {
    System.exit(0); 
}

Upvotes: 1

copeg
copeg

Reputation: 8348

See the API for Frame - for the WINDOW_CLOSING event: "If the program doesn't explicitly hide or dispose the window while processing this event, the window close operation is canceled." - if you move your logic into the windowClosing method the application should exit. A Swing JFrame is a bit more friendly (you can set the default closing behavior to exit on close if you wish to exit the application upon closing the JFrame without the need for the WindowListener).

Last but not least, I'd recommend creating your GUI on the EventDispatchThread. For instance:

public static void main(String [] args) throws Exception{
    Runnable t = new Runnable(){
        public void run(){
            new AWTCounter();
        }
    };
    SwingUtilities.invokeAndWait(t);
}

Upvotes: 3

Related Questions