Java Swing: adding confirmation dialogue for closing Window in JFrame
2 posters
Page 1 of 1
Java Swing: adding confirmation dialogue for closing Window in JFrame
hello there
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
Hope you like this tutorial and it will come in handy for you
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
Hope you like this tutorial and it will come in handy for you
Re: Java Swing: adding confirmation dialogue for closing Window in JFrame
Thats a real good trick you got! I preserve rpp for these kind of stuffs.
BIT0102-Mohaimin- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
Similar topics
» Java Swing Example
» Java swing Layout presentation
» Java Tutorial: Painting in swing components, part 1
» Java Tutorial: Painting in swing components, part 2 (Interactive painting)
» Java Tutorial: Using Two Java Reference Keywords, this and super
» Java swing Layout presentation
» Java Tutorial: Painting in swing components, part 1
» Java Tutorial: Painting in swing components, part 2 (Interactive painting)
» Java Tutorial: Using Two Java Reference Keywords, this and super
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford