Results 1 to 11 of 11
  1. #1
    hemanthjava is offline Member
    Join Date
    Dec 2007
    Posts
    22
    Rep Power
    0

    Default 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);
    	}
    
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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();
        }
    }

  4. #4
    hemanthjava is offline Member
    Join Date
    Dec 2007
    Posts
    22
    Rep Power
    0

    Default

    I changed to the YES_OPTION only. But I still experience the same probelm

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  6. #6
    hemanthjava is offline Member
    Join Date
    Dec 2007
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    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();
        }
    }
    This snippet does not solve the purpose of Closing the JFrame conditionally. Can you please help in this regard ?

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You have done the same error too here.

    Java Code:
    setDefaultCloseOperation(EXIT_ON_CLOSE);

  8. #8
    hemanthjava is offline Member
    Join Date
    Dec 2007
    Posts
    22
    Rep Power
    0

    Default

    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.

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

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    cross-posted. thanks for wasting my time.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •