[ajug-members] preventing window close

Rob Manning manningr at spamcop.net
Thu Sep 16 11:26:58 EDT 2004



Quoting John Wells <jb at sourceillustrated.com>:

> Guys,
>
> Trying to prevent a JFrame from being closed.  I have the following code:
>
> frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
>
> frame.addWindowListener(new WindowAdapter(){
>   public void windowClosing (WindowEvent e)
>   {
>     System.out.println("Can't close me!");
>   }
>
>   public void windowIconified(WindowEvent e)
>   {
>     System.out.println("I'm iconifying...");
>   }
> });
>
> On both Windows and Linux, I'm able to see the windowIconified event, but
> windowClosing never fires.  Is there an esoteric trick to doing this I'm
> unaware of?
>

John,

Not sure why that would be.  The following performs according to the API on
FC2 linux w/ Sun JDK 1.4.2_05 :

import javax.swing.*;
import java.awt.event.*;

public class FrameTest {

    public static void main (String [] args) {
        JFrame myframe = new JFrame();

        myframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        myframe.addWindowListener(new WindowAdapter(){
            public void windowClosing (WindowEvent e) {
                System.out.println("Can't close me!");
            }

            public void windowIconified(WindowEvent e) {
                System.out.println("I'm iconifying...");
            }
        });
        myframe.setVisible(true);
    }
}

That is to say, when I click on the 'x' to close the window, I see this on the
command-line:

[manningr at tcs008903 manningr]$ java FrameTest
Can't close me!
Can't close me!
Can't close me!


Rob Manning



More information about the ajug-members mailing list