Results 1 to 11 of 11
Thread: JFrame Conditional Close
- 06-29-2008, 06:37 AM #1
Member
- Join Date
- Dec 2007
- Posts
- 22
- Rep Power
- 0
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.
Java 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); } }
- 06-29-2008, 06:59 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You have to use YES_OPTION only. Got it?
- 06-29-2008, 07:03 AM #3
Java 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(); } }
- 06-29-2008, 07:03 AM #4
Member
- Join Date
- Dec 2007
- Posts
- 22
- Rep Power
- 0
I changed to the YES_OPTION only. But I still experience the same probelm
- 06-29-2008, 07:08 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 06-29-2008, 07:12 AM #6
Member
- Join Date
- Dec 2007
- Posts
- 22
- Rep Power
- 0
- 06-29-2008, 07:13 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You have done the same error too here.
Java Code:setDefaultCloseOperation(EXIT_ON_CLOSE);
- 06-29-2008, 07:13 AM #8
Member
- Join Date
- Dec 2007
- Posts
- 22
- Rep Power
- 0
- 06-29-2008, 07:16 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
-
cross-posted. thanks for wasting my time.
- 06-29-2008, 07:49 AM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
Similar Threads
-
How to close an open JFrame window from a jsp page?
By kasisaiganesh in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 05-27-2008, 06:29 PM -
close a frame..
By tajinvillage in forum New To JavaReplies: 5Last Post: 04-27-2008, 10:22 PM -
How to close a JFrame
By valery in forum New To JavaReplies: 1Last Post: 08-06-2007, 05:33 PM -
Close a program in java
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 07:22 PM -
How to close JDBC Connection
By Heather in forum JDBCReplies: 2Last Post: 07-15-2007, 01:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks