Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-29-2008, 07:37 AM
Member
 
Join Date: Dec 2007
Posts: 14
hemanthjava is on a distinguished road
JFrame Conditional Close
I want the JFrame to be closed conditionally, based on a yes, no or cancel dialog.

I am not able to control it for "Cancel" and "No" button.s. Every time the JFrame Closes.

Code:
import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JOptionPane; // Displaying a JFrame at the center of the screen using Java Toolkit Class public class jFrameDemo extends JFrame { jFrameDemo() { setTitle("Center a Frame on Screen"); // jframe title setSize(400, 300); // jframe size this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addWindowListener(new WindowAdapter() { // java jframe close public void windowClosing(WindowEvent e) { if (JOptionPane.showConfirmDialog(null, "Are you sure ?") == JOptionPane.YES_NO_OPTION) { setVisible(false); dispose(); // jframe exit } else { } } }); } public static void main(String[] args) { jFrameDemo cfd = new jFrameDemo(); cfd.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-29-2008, 07:59 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
You have to use YES_OPTION only. Got it?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-29-2008, 08:03 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,144
hardwired is on a distinguished road
Code:
import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JOptionPane; public class FrameDemoRx extends JFrame { FrameDemoRx() { setSize(200, 200); setDefaultCloseOperation(EXIT_ON_CLOSE); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { int n = 99; do { n = JOptionPane.showConfirmDialog(null, "Are you sure ?"); String s = "unknown"; if(n == JOptionPane.YES_OPTION) s = "YES_OPTION = " + JOptionPane.YES_OPTION; if(n == JOptionPane.NO_OPTION) s = "NO_OPTION = " + JOptionPane.NO_OPTION; if(n == JOptionPane.CANCEL_OPTION) s = "CANCEL_OPTION = " + JOptionPane.CANCEL_OPTION; if(n == JOptionPane.CLOSED_OPTION) s = "CLOSED_OPTION = " + JOptionPane.CLOSED_OPTION; System.out.println("s = " + s); } while(n != -1); } }); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new FrameDemoRx(); } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-29-2008, 08:03 AM
Member
 
Join Date: Dec 2007
Posts: 14
hemanthjava is on a distinguished road
I changed to the YES_OPTION only. But I still experience the same probelm
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-29-2008, 08:08 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Oops, you have another mistake too. You have set the default close operation to exit. So what happened is, what ever the option you selected at the time frame has a process to complete. So change the default close operation to DO_NOTHING_ON_CLOSE. It should work.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-29-2008, 08:12 AM
Member
 
Join Date: Dec 2007
Posts: 14
hemanthjava is on a distinguished road
Quote:
Originally Posted by hardwired View Post
Code:
import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JOptionPane; public class FrameDemoRx extends JFrame { FrameDemoRx() { setSize(200, 200); setDefaultCloseOperation(EXIT_ON_CLOSE); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { int n = 99; do { n = JOptionPane.showConfirmDialog(null, "Are you sure ?"); String s = "unknown"; if(n == JOptionPane.YES_OPTION) s = "YES_OPTION = " + JOptionPane.YES_OPTION; if(n == JOptionPane.NO_OPTION) s = "NO_OPTION = " + JOptionPane.NO_OPTION; if(n == JOptionPane.CANCEL_OPTION) s = "CANCEL_OPTION = " + JOptionPane.CANCEL_OPTION; if(n == JOptionPane.CLOSED_OPTION) s = "CLOSED_OPTION = " + JOptionPane.CLOSED_OPTION; System.out.println("s = " + s); } while(n != -1); } }); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new FrameDemoRx(); } }
This snippet does not solve the purpose of Closing the JFrame conditionally. Can you please help in this regard ?
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-29-2008, 08:13 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
You have done the same error too here.

Code:
setDefaultCloseOperation(EXIT_ON_CLOSE);
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-29-2008, 08:13 AM
Member
 
Join Date: Dec 2007
Posts: 14
hemanthjava is on a distinguished road
Quote:
Originally Posted by Eranga View Post
Oops, you have another mistake too. You have set the default close operation to exit. So what happened is, what ever the option you selected at the time frame has a process to complete. So change the default close operation to DO_NOTHING_ON_CLOSE. It should work.
Quote:
Originally Posted by Eranga View Post
You have done the same error too here.

Code:
setDefaultCloseOperation(EXIT_ON_CLOSE);
Thank you so much. It worked for me. :-)
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-29-2008, 08:16 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Nice to hear that.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 06-29-2008, 08:46 AM
Senior Member
 
Join Date: Jun 2008
Posts: 194
Fubarable is on a distinguished road
cross-posted. thanks for wasting my time.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 06-29-2008, 08:49 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,039
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Don't worry Fubarable. Some people doesn't believe what we says at once.

Anyway, you are on the suns' forum too, isn't it?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to close an open JFrame window from a jsp page? kasisaiganesh JavaServer Pages (JSP) and JSTL 1 05-27-2008 07:29 PM
close a frame.. tajinvillage New To Java 5 04-27-2008 11:22 PM
How to close a JFrame valery New To Java 1 08-06-2007 06:33 PM
Close a program in java romina New To Java 1 07-25-2007 08:22 PM
How to close JDBC Connection Heather Database 2 07-15-2007 02:07 PM


All times are GMT +3. The time now is 07:01 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org