I have to change the normal behavior of a class that extend *JFrame.

The new behavior consists in the fact that when the user click on the "X" button the window will not close but it is minimized in the toolbar or the operating system.

So I have a class named MainFrame that extend JFrame and originally is something like this:

public class MainFrame extends JFrame {

    public MainFrame() {

        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

        // do something
    }

    // OTHER METHODS()   
}

So from what I have understand to obtain this behavior I have to change the previous line:

setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

into:

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

and in this way the window is not closed when the user click on the X button, then I have to add a listener that minimize the window when the user click on the X button, something like this one:

addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosing(WindowEvent e) {
        ((JFrame)e.getSource()).setState(JFrame.ICONIFIED);
    }
});

I think that this should work but my problem is that I don't know where I have to put it !!!

I tryed to put it into my constructor but Eclipse mark me some error message:

- WindowAdapter cannot be resolved to a type
- The method addWindowListener(WindowListener) in the type Window is not applicable for the arguments (new WindowAdapter()
 {})

Why? What am I missing? How can I solve?

1

There are 1 answers

1
asaini007 On

Did you import both of the following?

java.awt.event.WindowAdapter;
java.awt.event.WindowEvent;

Especially WindowAdapter...

Also, to your question of where to put the addWindowListener() method, the constructor would be appropriate.