Java Swing: adding confirmation dialogue for closing Window in JFrame

View previous topic View next topic Go down

Java Java Swing: adding confirmation dialogue for closing Window in JFrame

Post by BIT0122-Amit on Tue Apr 20, 2010 11:53 pm

hello there Smile

For novice java swing programmers, I am sure that you wanted to add a confirmation dialogue in your JFrame when the user clicks on the close button, right?

That sounds greek.

To speak simply, you have created a swing window. And when the user clicks on close button, you want it to ask,
"Are you sure you want to close this window?"

Somewhat like this:



it is very easy actually. All you have to do (after creating your Jframe window)is to
-add a windowListener method in your JFrame
-use WindowAdapter inside to monitor WindowClosing
-override the WindowClosing method to do your desired thing.

Now, lets get to the code:

I hope you already know how to make windows in Jframe, but here is a short code to make one.


see this part?
We are telling the Jframe not to do anything when the close button is clicked on its own.
To put simply, we are taking the control away.

now, lets add the window Listener, window adapter, and overriding the window closing part:



I have created a static method called exit which looks somewhat like this:

there you have it Very Happy
Hope you like this tutorial and it will come in handy for you

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Java Re: Java Swing: adding confirmation dialogue for closing Window in JFrame

Post by BIT0102-Mohaimin on Tue Mar 22, 2011 12:15 am

Thats a real good trick you got! I preserve rpp for these kind of stuffs.
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum