3 Replies - 2790 Views - Last Post: 30 December 2009 - 04:01 PM Rate Topic: -----

#1 abgorn  Icon User is offline

  • sudo apt-get install brain
  • member icon

Reputation: 30
  • View blog
  • Posts: 1,410
  • Joined: 05-June 08

Stopping a JFrame from Closing

Posted 30 December 2009 - 02:00 PM

I have a problem were I have written a JFrame which opens a JOptionPane to ask the user if they wants to close the window, but when the WindowListener is called, the window is gonna close whether I like it or not and there is no other way (I have found after extensive searching) to find if the user pressed the close button and the only solution I've found is to remake the window like so:

import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;

public class MyFrame extends JFrame {

    static MainWindow mw;

    public static void main(String[] args) throws UnsupportedLookAndFeelException,
            ClassNotFoundException, InstantiationException, IllegalAccessException {
        //Set the system look & feel
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                mw = new MainWindow();
            }
        });
    }
}

class MainWindow extends JFrame {

    public MainWindow() {
        // I didn't use the setDefaultCloseOperation because I'd just be using
        // the HIDE_ON_CLOSE value anyway
        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                String[] ds = {"Cancel", "No", "Yes"};

                int response = JOptionPane.showOptionDialog(null,
                        "You sure you wanna exit?",
                        "", JOptionPane.YES_NO_CANCEL_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null, ds, null);
                if (response == 0) {
                    // The window closes anyway so I have to open a new version
                    // of the same type of window
                    new MainWindow();
                } else if (response == 1) {
                    // The window closes anyway so I have to open a new version
                    // of the same type of window
                    new MainWindow();
                } else if (response == 2) {
                    System.exit(0);
                }
            }
        });

        setSize(500, 400);
        setVisible(true);
    }
}



I can make due with this if I must but it looks really bad when you click "No" and it closes and then re-opens the frame.

Is This A Good Question/Topic? 0
  • +

Replies To: Stopping a JFrame from Closing

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: Stopping a JFrame from Closing

Posted 30 December 2009 - 02:11 PM

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)

on windowClosing() just return if the user do not want to close else
this.dispose();
System.exit(0);
Was This Post Helpful? 1
  • +
  • -

#3 abgorn  Icon User is offline

  • sudo apt-get install brain
  • member icon

Reputation: 30
  • View blog
  • Posts: 1,410
  • Joined: 05-June 08

Re: Stopping a JFrame from Closing

Posted 30 December 2009 - 02:25 PM

I feel pretty stupid but thank you, you're a fantastic help :)
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,033
  • Joined: 15-July 08

Re: Stopping a JFrame from Closing

Posted 30 December 2009 - 04:01 PM

Hey, also doing this is normally perfectly acceptable on a NORMAL method, but for main is generally not a good idea.
public static void main(String[] args) throws UnsupportedLookAndFeelException,
			ClassNotFoundException, InstantiationException, IllegalAccessException {



Instead, use try { } catch (Exception). This is to prevent unhandled errors from occurring in main.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1