NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

JFrame.setDefaultCloseOperation() not behaving properly?

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users
View previous topic :: View next topic  
Author Message
shimniok



Joined: 11 Oct 2010
Posts: 8
Location: Colorado

PostPosted: Wed Oct 20, 2010 4:35 am    Post subject: JFrame.setDefaultCloseOperation() not behaving properly? Reply with quote

Hi, hoping the gurus here may be able to help this newb.

I'm attempting to intercept a close window event in my Swing desktop application (testing on Windows at the moment) so I can prompt the user to save a file (yes/no/cancel). The code that should work, doesn't. What am I goofing up?

Code:
public class WheelEncoderGeneratorView extends FrameView {

    public WheelEncoderGeneratorView(SingleFrameApplication app) {
        super(app);

        System.out.println("Initializing components...");
        initComponents();
        registerForMacOSXEvents(); // OSX-specific setup

        // Handle window close event
        getFrame().setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        getFrame().addWindowListener(new CloseListener());
//...
        System.out.println("Done with View initialization...");
    }

//...

    private class CloseListener extends WindowAdapter {
        @Override
        public void windowClosing(WindowEvent event) {
            System.out.println("windowClosing() -- enter");
            if (promptSaveFirst()) {
                System.out.println("windowClosing() -- disposing of window");
                getFrame().dispose();
            } else {
                System.out.println("windowClosing() -- cancelling close");
            }
            System.out.println("windowClosing() -- exit");
            //System.exit(0);
        }
    }
}


Output from running the app, then clicking the close box, then selecting "Cancel" is:

Quote:
Done with View initialization...
windowClosing() -- enter
promptSaveFirst() -- return false // indicates a 'cancel' was selected
windowClosing() -- cancelling close
windowClosing() -- exit


In short, the logic is right and the code is taking the right path for canceling the close action. But the app window closes and the process terminates anyway. The setDefaultCloseOperation with DO_NOTHING_ON_CLOSE isn't behaving like I expected.

I *thought* what was supposed to happen was... nothing. The app should stay running unless I explicitly call System.exit() or dispose() right? I'm not calling either. And the app still closes / terminates.

I'm I calling setDefaultCloseOperation() on the wrong object?

Thanks in advance,

Michael
Back to top
shimniok



Joined: 11 Oct 2010
Posts: 8
Location: Colorado

PostPosted: Thu Oct 21, 2010 4:27 am    Post subject: Reply with quote

PS: Same incorrect behavior on OS X ...

Makes me think that I am setting default close operation on the wrong thing or in the wrong way... hmmm.
Back to top
shimniok



Joined: 11 Oct 2010
Posts: 8
Location: Colorado

PostPosted: Thu Oct 21, 2010 6:17 am    Post subject: Reply with quote

If I include this field in the FrameView class, then I get the correct DO_NOTHING_ON_CLOSE behavior. At least on OSX (haven't tested win yet).

Code:
    private JFrame mainFrame = WheelEncoderGeneratorApp.getApplication().getMainFrame();


The variable is unused. Yet if I remove it or replace it with, say, this.getFrame(), the DO_NOTHING_ON_CLOSE behavior breaks.

I find this utterly bizarre and nonsensical.
Back to top
Zelig



Joined: 04 Apr 2009
Posts: 64
Location: FRANCE

PostPosted: Fri Oct 22, 2010 10:35 am    Post subject: Reply with quote

Hello,

You shouldn't try to prevent the windows from closing in the "close" function. In the FrameView class, you have a function called "CanExit".

Any test which could lead to resuming the application should be written here. This method returns a boolean, true if the application is supposed to close or false if the application should go on running.
Back to top
shimniok



Joined: 11 Oct 2010
Posts: 8
Location: Colorado

PostPosted: Fri Oct 22, 2010 4:32 pm    Post subject: Reply with quote

@Zelig: Great tip, thanks! Did a little searching and found this useful explanation:

http://forums.sun.com/thread.jspa?messageID=10228903

See post 12 in the link above. I implemented mine a little differently. I kept all the UI stuff in the View and simply called quit() on it from within the exit listener. That is...

Code:
public class WheelEncoderGeneratorApp extends SingleFrameApplication implements Application.ExitListener
{
    protected WheelEncoderGeneratorView view;

    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        addExitListener(this);
        show(view = new WheelEncoderGeneratorView(this));
    }

    @Override public boolean canExit(java.util.EventObject e) {
        System.out.println("WheelEncoderGeneratorApp.canExit(): calling view.quit()");
        return view.quit();
    }

    @Override public void willExit(java.util.EventObject e) {
    }
// ...



And then:

Code:
public class WheelEncoderGeneratorView extends FrameView {
//...
    public boolean quit()
    {
        return promptSaveFirst();
    }
//...


I had to first disable the the previous solution's addWindowListener() and setDefaultCloseOperation() calls.

I'm still not sure why the previous solution only worked when declaring that private instance variable with the call to myApp.getApplication().getMainFrame() ... I really don't like inexplicable fixes of that sort, y'know??

Anyway the canExit() bit works great on Windows.

EDIT: works perfectly on OSX, too.

Thanks again!
Back to top
shimniok



Joined: 11 Oct 2010
Posts: 8
Location: Colorado

PostPosted: Fri Oct 29, 2010 4:41 pm    Post subject: Reply with quote

Here's a hint as to why I was seeing bizarre behavior with calling: WheelEncoderGeneratorApp.getApplication().getMainFrame();

http://markmail.org/message/4no3q2jc46ldgs6c
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo