Note that there are some explanatory texts on larger screens.

plurals
  1. POMinimize the window (and not to close it) when the user click on the “x” button? What is wrong in this Java Swing code?
    primarykey
    data
    text
    <p>I have to change the normal behavior of a class that extend *<em>JFrame</em>.</p> <p>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.</p> <p>So I have a class named <strong>MainFrame</strong> that extend <strong>JFrame</strong> and originally is something like this:</p> <pre><code>public class MainFrame extends JFrame { public MainFrame() { setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); // do something } // OTHER METHODS() } </code></pre> <p>So from what I have understand to obtain this behavior I have to change the previous line:</p> <pre><code>setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); </code></pre> <p>into:</p> <pre><code>setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); </code></pre> <p>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:</p> <pre><code>addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { ((JFrame)e.getSource()).setState(JFrame.ICONIFIED); } }); </code></pre> <p>I think that this should work but my problem is that I don't know where I have to put it !!!</p> <p>I tryed to put it into my constructor but Eclipse mark me some error message:</p> <pre><code>- WindowAdapter cannot be resolved to a type - The method addWindowListener(WindowListener) in the type Window is not applicable for the arguments (new WindowAdapter() {}) </code></pre> <p>Why? What am I missing? How can I solve?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload