IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Java Swing: adding confirmation dialogue for closing Window in JFrame

2 posters

Go down

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

Post by BIT0122-Amit 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:

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

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
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

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

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

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

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

Back to top

- Similar topics

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