closing a specific JFrame of several

  • Follow


I have a JFrame that, when I click "new" in the menu bar, creates
another JFrame like that of the first. It just runs the following code
....

EventQueue.invokeLater(new Runnable() {
   public void run() {
      MyApp app = new MyApp();
      app.createGUI();
   }
});

And inside the createGUI() method is this ...

addWindowListener(new WindowListener() {
   public void windowOpened(WindowEvent arg0) {}
   public void windowClosed(WindowEvent arg0) {}
   public void windowIconified(WindowEvent arg0) {}
   public void windowDeiconified(WindowEvent arg0) {}
   public void windowActivated(WindowEvent arg0) {}
   public void windowDeactivated(WindowEvent arg0) {}
   public void windowClosing(WindowEvent arg0) {
      System.exit(0);
   }
});

As it works right now, when I close a window, all the windows close and
I'd just like to close only that specific window in which the user
clicked the close button.

Any help much appreciated.

0
Reply Wizumwalt (114) 12/18/2006 10:26:59 PM

William Z. wrote:
> I have a JFrame that, when I click "new" in the menu bar, creates
> another JFrame like that of the first. It just runs the following code
> ...
>
> EventQueue.invokeLater(new Runnable() {
>    public void run() {
>       MyApp app = new MyApp();
>       app.createGUI();
>    }
> });
>
> And inside the createGUI() method is this ...
>
> addWindowListener(new WindowListener() {
>    public void windowOpened(WindowEvent arg0) {}
>    public void windowClosed(WindowEvent arg0) {}
>    public void windowIconified(WindowEvent arg0) {}
>    public void windowDeiconified(WindowEvent arg0) {}
>    public void windowActivated(WindowEvent arg0) {}
>    public void windowDeactivated(WindowEvent arg0) {}
>    public void windowClosing(WindowEvent arg0) {
>       System.exit(0);
>    }
> });
>
> As it works right now, when I close a window, all the windows close and
> I'd just like to close only that specific window in which the user
> clicked the close button.
>
> Any help much appreciated.

The System.exit(0); call tells the JVM to terminate the program....
Don't use the addWindowListener at all, instead what you really want is
to call
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

Hope this helps,
Daniel.

0
Reply Daniel 12/18/2006 11:03:27 PM


But that would just close the windows and leave the VM running. Should
I keep a running count of open frames and when 0, call System.exit()?

>
> The System.exit(0); call tells the JVM to terminate the program....
> Don't use the addWindowListener at all, instead what you really want is
> to call
> setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
> 
> Hope this helps,
> Daniel.

0
Reply William 12/18/2006 11:31:34 PM

William Z. wrote:
> > The System.exit(0); call tells the JVM to terminate the program....
> > Don't use the addWindowListener at all, instead what you really want is
> > to call
> > setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
> >
> > Hope this helps,
> > Daniel.
> But that would just close the windows and leave the VM running. Should
> I keep a running count of open frames and when 0, call System.exit()?
Please don't top post... Makes following conversations difficult. :-)

The Event Dispatch Thread (EDT) should exit when all JFrames have been
disposed, and the JVM should exit when all the non-daemon (including
the EDT) threads exit.

So, you shouldn't have to keep track of all open frames.

0
Reply Daniel 12/19/2006 12:09:01 AM

William Z. wrote:
> I have a JFrame that, when I click "new" in the menu bar, creates
> another JFrame like that of the first. It just runs the following code
> ...
> 
> EventQueue.invokeLater(new Runnable() {
>    public void run() {
>       MyApp app = new MyApp();
>       app.createGUI();
>    }
> });
> 
> And inside the createGUI() method is this ...
> 
> addWindowListener(new WindowListener() {
>    public void windowOpened(WindowEvent arg0) {}
>    public void windowClosed(WindowEvent arg0) {}
>    public void windowIconified(WindowEvent arg0) {}
>    public void windowDeiconified(WindowEvent arg0) {}
>    public void windowActivated(WindowEvent arg0) {}
>    public void windowDeactivated(WindowEvent arg0) {}
>    public void windowClosing(WindowEvent arg0) {
>       System.exit(0);
>    }
> });
> 
> As it works right now, when I close a window, all the windows close and
> I'd just like to close only that specific window in which the user
> clicked the close button.
> 
> Any help much appreciated.
> 

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

public class test extends JFrame implements ActionListener {
     JButton openButton,closeButton;
     JFrame frame;

     public test() {
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setLayout(new FlowLayout());

         openButton = new JButton("Open");
         openButton.addActionListener(this);
         add(openButton);

         closeButton = new JButton("Close");
         closeButton.setEnabled(false);
         closeButton.addActionListener(this);
         add(closeButton);

         frame = new JFrame("extra frame");
         frame.addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent we) {
                 test.this.actionPerformed(new ActionEvent(
                  closeButton,ActionEvent.ACTION_PERFORMED,"Close"));
             }
         });
         frame.setSize(400,300);
         frame.setLocationRelativeTo(null);

         pack();
         setVisible(true);
     }

     public void actionPerformed(ActionEvent ae) {
         String ac = ae.getActionCommand();

         if (ac.equals("Open")) {
             openButton.setEnabled(false);
             frame.setVisible(true);
             closeButton.setEnabled(true);
         } else if (ac.equals("Close")) {
             closeButton.setEnabled(false);
             frame.setVisible(false);
             openButton.setEnabled(true);
         }
     }

     public static void main(String[] args) {
         Runnable r = new Runnable() {
             public void run() {
                 new test();
             }
         };
         EventQueue.invokeLater(r);
     }
}

-- 

Knute Johnson
email s/nospam/knute/
0
Reply Knute 12/19/2006 3:29:59 AM

4 Replies
121 Views

(page loaded in 2.3 seconds)

Similiar Articles:













7/29/2012 11:50:42 AM


Reply: