Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

in simple TextEditor how to stop Closing ?

amin_xzxamin_xzx Member Posts: 11

Hi
I write simple textEditor ,but i want when closing(click Close () [ beside (-)(□)()) ] or Alt+F4.
Like notPad, display Question to save. and click Cancel not clossing.
I know JOptionPane.* , i want prevant to closing when click in cancel.

please help help me ...
thanks.

Comments

  • arb123arb123 Member Posts: 696
    [code]
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;

    import javax.swing.JFrame;
    import javax.swing.JOptionPane;

    public class AreYouSure extends JFrame {

    public AreYouSure() {
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent event) {
    onStateChange(event);
    }
    });

    }

    protected void onStateChange(WindowEvent event) {
    if (WindowEvent.WINDOW_CLOSING == event.getID()) {
    int choice = JOptionPane.showConfirmDialog(this, "Save?");
    if (choice == JOptionPane.YES_OPTION) {
    saveFile();
    exitApplication();
    } else if (choice == JOptionPane.NO_OPTION) {
    exitApplication();
    }
    //else its a cancel, do nothing
    }
    }

    private void saveFile() {
    //TODO
    }

    private void exitApplication() {
    hide();
    dispose();
    System.exit(0);
    }

    public static void main(String[] args) {
    new AreYouSure().show();
    }
    }
    [/code]

    ---------------------------------
    [size=1](Its just my sig)
    HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

Sign In or Register to comment.