DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 6 of 6

Hybrid View

  1. #1
    Setya Guest

    How to cancel form closing using WindowAdapter ?


    Hi all,
    I've just started my first Java application. In my app, I inherits JFrame.
    When the user close the form with close button, I want to show a Yes/No dialog
    to give user a chance to cancel the form closing, but I can not find any
    methods/properties in the event object passed to the WindowClosing method
    that I can use to cancel the form closing.

    Any advice would be greatly appreciated.

    Thanks in advance


    Setya

  2. #2
    Kent Guest

    Re: How to cancel form closing using WindowAdapter ?


    Hi,

    Just use a WindowListener as such:

    private static class MyWindowListener extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
    boolean closeWindow = false; //get user input into this boolean

    if (closeWindow) {
    //now close the window - System.exit(0) etc.
    }
    }
    }


    Add this listener to your window with this in the constructor code:

    this.addWindowListener(new MyWindowListener());

    Regards,
    Kent


    "Setya" <jsetya@yahoo.com> wrote:
    >
    >Hi all,
    > I've just started my first Java application. In my app, I inherits JFrame.
    >When the user close the form with close button, I want to show a Yes/No

    dialog
    >to give user a chance to cancel the form closing, but I can not find any
    >methods/properties in the event object passed to the WindowClosing method
    >that I can use to cancel the form closing.
    >
    >Any advice would be greatly appreciated.
    >
    >Thanks in advance
    >
    >
    >Setya



  3. #3
    Setya Guest

    Re: How to cancel form closing using WindowAdapter ?


    "Kent" <kb@essential.com.au> wrote:
    >
    >Hi,
    >
    >Just use a WindowListener as such:
    >
    >private static class MyWindowListener extends WindowAdapter {
    > public void windowClosing(WindowEvent e) {
    > boolean closeWindow = false; //get user input into this boolean
    >
    > if (closeWindow) {
    > //now close the window - System.exit(0) etc.
    > }
    > }
    >}
    >
    >
    >Add this listener to your window with this in the constructor code:
    >
    >this.addWindowListener(new MyWindowListener());
    >
    >Regards,
    >Kent
    >
    >
    >"Setya" <jsetya@yahoo.com> wrote:
    >>
    >>Hi all,
    >> I've just started my first Java application. In my app, I inherits JFrame.
    >>When the user close the form with close button, I want to show a Yes/No

    >dialog
    >>to give user a chance to cancel the form closing, but I can not find any
    >>methods/properties in the event object passed to the WindowClosing method
    >>that I can use to cancel the form closing.
    >>
    >>Any advice would be greatly appreciated.
    >>
    >>Thanks in advance
    >>
    >>
    >>Setya

    >

    Thanks Mr. Kent,

    I write my code similiar to what you suggest above and I show a Yes/No dialog
    :

    int iSelect;
    iSelect=JOptionPane.showOptionDialog(null,"Are you sure you want to exit
    this application ?","Exit Application",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);

    if (iSelect==JOptionPane.YES_OPTION)
    {
    System.exit(0);
    }

    But when I click then NO button, the form keep closing anyway.

    I use Forte For Java as my IDE, does it make a difference ?

    Setya


  4. #4
    Kent Guest

    Re: How to cancel form closing using WindowAdapter ?


    Setya,

    Sorry, I forgot to mention you need to add this code in your constructor
    for the window class:

    this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

    This will stop the window from closing and but will still let your window
    listener code to run and close the window where appropriate.

    Regards,
    Kent


    "Setya" <jsetya@yahoo.com> wrote:
    >
    >"Kent" <kb@essential.com.au> wrote:
    >>
    >>Hi,
    >>
    >>Just use a WindowListener as such:
    >>
    >>private static class MyWindowListener extends WindowAdapter {
    >> public void windowClosing(WindowEvent e) {
    >> boolean closeWindow = false; //get user input into this boolean
    >>
    >> if (closeWindow) {
    >> //now close the window - System.exit(0) etc.
    >> }
    >> }
    >>}
    >>
    >>
    >>Add this listener to your window with this in the constructor code:
    >>
    >>this.addWindowListener(new MyWindowListener());
    >>
    >>Regards,
    >>Kent
    >>
    >>
    >>"Setya" <jsetya@yahoo.com> wrote:
    >>>
    >>>Hi all,
    >>> I've just started my first Java application. In my app, I inherits

    JFrame.
    >>>When the user close the form with close button, I want to show a Yes/No

    >>dialog
    >>>to give user a chance to cancel the form closing, but I can not find any
    >>>methods/properties in the event object passed to the WindowClosing method
    >>>that I can use to cancel the form closing.
    >>>
    >>>Any advice would be greatly appreciated.
    >>>
    >>>Thanks in advance
    >>>
    >>>
    >>>Setya

    >>

    >Thanks Mr. Kent,
    >
    >I write my code similiar to what you suggest above and I show a Yes/No dialog
    >:
    >
    >int iSelect;
    >iSelect=JOptionPane.showOptionDialog(null,"Are you sure you want to exit
    >this application ?","Exit Application",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
    >
    >if (iSelect==JOptionPane.YES_OPTION)
    > {
    > System.exit(0);
    > }
    >
    >But when I click then NO button, the form keep closing anyway.
    >
    >I use Forte For Java as my IDE, does it make a difference ?
    >
    >Setya
    >



  5. #5
    Setya Guest

    Re: How to cancel form closing using WindowAdapter ?


    "Kent" <kb@essential.com.au> wrote:
    >
    >Setya,
    >
    >Sorry, I forgot to mention you need to add this code in your constructor
    >for the window class:
    >
    >this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    >
    >This will stop the window from closing and but will still let your window
    >listener code to run and close the window where appropriate.
    >
    >Regards,
    >Kent
    >
    >
    >"Setya" <jsetya@yahoo.com> wrote:
    >>
    >>"Kent" <kb@essential.com.au> wrote:
    >>>
    >>>Hi,
    >>>
    >>>Just use a WindowListener as such:
    >>>
    >>>private static class MyWindowListener extends WindowAdapter {
    >>> public void windowClosing(WindowEvent e) {
    >>> boolean closeWindow = false; //get user input into this boolean
    >>>
    >>> if (closeWindow) {
    >>> //now close the window - System.exit(0) etc.
    >>> }
    >>> }
    >>>}
    >>>
    >>>
    >>>Add this listener to your window with this in the constructor code:
    >>>
    >>>this.addWindowListener(new MyWindowListener());
    >>>
    >>>Regards,
    >>>Kent
    >>>
    >>>
    >>>"Setya" <jsetya@yahoo.com> wrote:
    >>>>
    >>>>Hi all,
    >>>> I've just started my first Java application. In my app, I inherits

    >JFrame.
    >>>>When the user close the form with close button, I want to show a Yes/No
    >>>dialog
    >>>>to give user a chance to cancel the form closing, but I can not find

    any
    >>>>methods/properties in the event object passed to the WindowClosing method
    >>>>that I can use to cancel the form closing.
    >>>>
    >>>>Any advice would be greatly appreciated.
    >>>>
    >>>>Thanks in advance
    >>>>
    >>>>
    >>>>Setya
    >>>

    >>Thanks Mr. Kent,
    >>
    >>I write my code similiar to what you suggest above and I show a Yes/No

    dialog
    >>:
    >>
    >>int iSelect;
    >>iSelect=JOptionPane.showOptionDialog(null,"Are you sure you want to exit
    >>this application ?","Exit Application",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
    >>
    >>if (iSelect==JOptionPane.YES_OPTION)
    >> {
    >> System.exit(0);
    >> }
    >>
    >>But when I click then NO button, the form keep closing anyway.
    >>
    >>I use Forte For Java as my IDE, does it make a difference ?
    >>
    >>Setya
    >>

    >

    Mr. Kent,

    So this is why the form keeps closing even if I choose NO.

    My previous code was :

    this.setDefaultCloseOperation(WindowConstants.CLOSE_ON_EXIT);


    Thanks for your help.


    Setya

  6. #6
    Volker Held Guest

    Re: How to cancel form closing using WindowAdapter ?

    Hi,

    This should solve the problem, though itīs not a good app - the most
    important part for you is the inner class implementing
    the ActionListener interface. There you buttons action will be handled
    correctly - this of course works only for one button since
    i do not check the buttons action command.

    Hope it helps you out....

    Volker Held

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

    public class ConfirmClose extends JFrame{
    public static void main( String[] args){ new ConfirmClose(); }

    public ConfirmClose(){
    super("WindowListening");

    setSize(200,200);
    setLocation(200,200);

    JButton jb = new JButton("Close");
    jb.addActionListener(new NewActionAdapter());

    getContentPane().add(jb);

    setVisible(true);

    pack();
    }

    class NewActionAdapter implements ActionListener{
    public void actionPerformed( ActionEvent ae ){
    if ( JOptionPane.OK_OPTION ==
    JOptionPane.showConfirmDialog(null, "You are sure you wanna quit?"
    , "Closing application", JOptionPane.OK_CANCEL_OPTION))
    System.exit(0);
    }
    }
    }
    "Setya" <jsetya@yahoo.com> wrote in message news:3cb67481@10.1.10.29...
    >
    > "Kent" <kb@essential.com.au> wrote:
    > >
    > >Setya,
    > >
    > >Sorry, I forgot to mention you need to add this code in your constructor
    > >for the window class:
    > >
    > >this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    > >
    > >This will stop the window from closing and but will still let your window
    > >listener code to run and close the window where appropriate.
    > >
    > >Regards,
    > >Kent
    > >
    > >
    > >"Setya" <jsetya@yahoo.com> wrote:
    > >>
    > >>"Kent" <kb@essential.com.au> wrote:
    > >>>
    > >>>Hi,
    > >>>
    > >>>Just use a WindowListener as such:
    > >>>
    > >>>private static class MyWindowListener extends WindowAdapter {
    > >>> public void windowClosing(WindowEvent e) {
    > >>> boolean closeWindow = false; file://get user input into this

    boolean
    > >>>
    > >>> if (closeWindow) {
    > >>> file://now close the window - System.exit(0) etc.
    > >>> }
    > >>> }
    > >>>}
    > >>>
    > >>>
    > >>>Add this listener to your window with this in the constructor code:
    > >>>
    > >>>this.addWindowListener(new MyWindowListener());
    > >>>
    > >>>Regards,
    > >>>Kent
    > >>>
    > >>>
    > >>>"Setya" <jsetya@yahoo.com> wrote:
    > >>>>
    > >>>>Hi all,
    > >>>> I've just started my first Java application. In my app, I inherits

    > >JFrame.
    > >>>>When the user close the form with close button, I want to show a

    Yes/No
    > >>>dialog
    > >>>>to give user a chance to cancel the form closing, but I can not find

    > any
    > >>>>methods/properties in the event object passed to the WindowClosing

    method
    > >>>>that I can use to cancel the form closing.
    > >>>>
    > >>>>Any advice would be greatly appreciated.
    > >>>>
    > >>>>Thanks in advance
    > >>>>
    > >>>>
    > >>>>Setya
    > >>>
    > >>Thanks Mr. Kent,
    > >>
    > >>I write my code similiar to what you suggest above and I show a Yes/No

    > dialog
    > >>:
    > >>
    > >>int iSelect;
    > >>iSelect=JOptionPane.showOptionDialog(null,"Are you sure you want to exit
    > >>this application ?","Exit

    Application",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
    > >>
    > >>if (iSelect==JOptionPane.YES_OPTION)
    > >> {
    > >> System.exit(0);
    > >> }
    > >>
    > >>But when I click then NO button, the form keep closing anyway.
    > >>
    > >>I use Forte For Java as my IDE, does it make a difference ?
    > >>
    > >>Setya
    > >>

    > >

    > Mr. Kent,
    >
    > So this is why the form keeps closing even if I choose NO.
    >
    > My previous code was :
    >
    > this.setDefaultCloseOperation(WindowConstants.CLOSE_ON_EXIT);
    >
    >
    > Thanks for your help.
    >
    >
    > Setya




Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links