Netbeans RCP: Asking before exiting the application

This article explains how to present a confirmation dialog asking if the user really wants to close the application.

This article was originally published on techtavern.wordpress.com.

The following solution registers a callable via @OnStop annotation. The callable is an opportunity to cancel closing the application by returning false.

import java.util.concurrent.Callable;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.modules.OnStop;

@OnStop
public class ApplicationOnStop implements Callable<Boolean> {
@Override
   public Boolean call() throws Exception {
      NotifyDescriptor d = new NotifyDescriptor.Confirmation("Close application?", NotifyDescriptor.YES_NO_OPTION)
      return DialogDisplayer.getDefault().notify(d) == NotifyDescriptor.YES_OPTION; 
   } 
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: