How come my program doesn't shut down when all windows are closed?

As of Java 1.5 (maybe earlier) java is supposed to exit when all windows have been closed. I have confirmed that this happens in a simple case of displaying 2 JFrames and closing both. Can anyone tell me why it's not happening in the case of 2 frames and 1 dialog here?
* Created on Jul 12, 2006 by @author Tom Jacobs
package tjacobs.ui;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class ModalOnParentDialog extends JDialog implements ComponentListener,
          WindowListener {
     private Component mOwner;
     private boolean mParentOnly = false;
     public ModalOnParentDialog() throws HeadlessException {
          super();
          init(null);
     public ModalOnParentDialog(Frame arg0) throws HeadlessException {
          super(arg0);
          init(arg0);
     public ModalOnParentDialog(Frame arg0, boolean arg1)
               throws HeadlessException {
          super(arg0, arg1);
          init(arg0);
     public ModalOnParentDialog(Frame arg0, String arg1)
               throws HeadlessException {
          super(arg0, arg1);
          init(arg0);
     public ModalOnParentDialog(Frame arg0, String arg1, boolean arg2)
               throws HeadlessException {
          super(arg0, arg1, arg2);
          init(arg0);
     public ModalOnParentDialog(Frame arg0, String arg1, boolean arg2,
               GraphicsConfiguration arg3) {
          super(arg0, arg1, arg2, arg3);
          init(arg0);
     public ModalOnParentDialog(Dialog arg0) throws HeadlessException {
          super(arg0);
          init(arg0);
     public ModalOnParentDialog(Dialog arg0, boolean arg1)
               throws HeadlessException {
          super(arg0, arg1);
          init(arg0);
     public ModalOnParentDialog(Dialog arg0, String arg1)
               throws HeadlessException {
          super(arg0, arg1);
          init(arg0);
     public ModalOnParentDialog(Dialog arg0, String arg1, boolean arg2)
               throws HeadlessException {
          super(arg0, arg1, arg2);
          init(arg0);
     public ModalOnParentDialog(Dialog arg0, String arg1, boolean arg2,
               GraphicsConfiguration arg3) throws HeadlessException {
          super(arg0, arg1, arg2, arg3);
          init(arg0);
     private void init(Component c) {
          mOwner = c;
          addComponentListener(this);
          addWindowListener(this);
     public void componentResized(ComponentEvent arg0) {}
     public void componentMoved(ComponentEvent arg0) {}
     public void componentShown(ComponentEvent arg0) {
          doModalOp();
     public void componentHidden(ComponentEvent arg0) {
          doModalOp();
     public boolean isParentOnly() {
          return mParentOnly;
     public void setParentOnly(boolean b) {
          mParentOnly = b;
     public void setModal(boolean b) {
          super.setModal(b);
          //mModal = b;
          doModalOp();
     private void doModalOp() {
          if (mOwner != null) {
               mOwner.setEnabled(!super.isModal() || !isVisible());
     public boolean isModal() {
          return super.isModal() && !mParentOnly;
//     public boolean isParentModel() {
//          return mModal;
     public void windowOpened(WindowEvent arg0) {
          doModalOp();
     public void windowClosing(WindowEvent arg0) {}
     public void windowClosed(WindowEvent arg0) {
          doModalOp();
     public void windowIconified(WindowEvent arg0) {}
     public void windowDeiconified(WindowEvent arg0) {}
     public void windowActivated(WindowEvent arg0) {}
     public void windowDeactivated(WindowEvent arg0) {}
     protected void finalize() throws Throwable {
          super.finalize();
          removeComponentListener(this);
          removeWindowListener(this);
          mOwner = null;
     public static void main(String[] args) {
          JFrame jf1 = new JFrame("1"), jf2 = new JFrame("2");
          final ModalOnParentDialog d = new ModalOnParentDialog(jf1, "3");
          d.setModal(true);
          d.setParentOnly(true);
          JButton b = new JButton("Show");
          b.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent ae) {
                    d.setVisible(!d.isVisible());
          jf1.setBounds(0,0,40,40);
          jf2.setBounds(40,40,40,40);
          jf2.add(b);
          d.setBounds(80,80,40,40);
          jf1.setVisible(true);
          jf2.setVisible(true);
          d.setVisible(true);
}

I fixed it
* Created on Jul 12, 2006 by @author Tom Jacobs
package tjacobs.ui;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class ModalOnParentDialog extends JDialog implements ComponentListener,
          WindowListener {
     private Component mOwner;
     private boolean mParentOnly = false;
     public ModalOnParentDialog() throws HeadlessException {
          super();
          init(null);
     public ModalOnParentDialog(Frame arg0) throws HeadlessException {
          super(arg0);
          init(arg0);
     public ModalOnParentDialog(Frame arg0, boolean arg1)
               throws HeadlessException {
          super(arg0, arg1);
          init(arg0);
     public ModalOnParentDialog(Frame arg0, String arg1)
               throws HeadlessException {
          super(arg0, arg1);
          init(arg0);
     public ModalOnParentDialog(Frame arg0, String arg1, boolean arg2)
               throws HeadlessException {
          super(arg0, arg1, arg2);
          init(arg0);
     public ModalOnParentDialog(Frame arg0, String arg1, boolean arg2,
               GraphicsConfiguration arg3) {
          super(arg0, arg1, arg2, arg3);
          init(arg0);
     public ModalOnParentDialog(Dialog arg0) throws HeadlessException {
          super(arg0);
          init(arg0);
     public ModalOnParentDialog(Dialog arg0, boolean arg1)
               throws HeadlessException {
          super(arg0, arg1);
          init(arg0);
     public ModalOnParentDialog(Dialog arg0, String arg1)
               throws HeadlessException {
          super(arg0, arg1);
          init(arg0);
     public ModalOnParentDialog(Dialog arg0, String arg1, boolean arg2)
               throws HeadlessException {
          super(arg0, arg1, arg2);
          init(arg0);
     public ModalOnParentDialog(Dialog arg0, String arg1, boolean arg2,
               GraphicsConfiguration arg3) throws HeadlessException {
          super(arg0, arg1, arg2, arg3);
          init(arg0);
     private void init(Component c) {
          mOwner = c;
          addComponentListener(this);
          addWindowListener(this);
     public void componentResized(ComponentEvent arg0) {}
     public void componentMoved(ComponentEvent arg0) {}
     public void componentShown(ComponentEvent arg0) {
          doModalOp();
     public void componentHidden(ComponentEvent arg0) {
          doModalOp();
     public boolean isParentOnly() {
          return mParentOnly;
     public void setParentOnly(boolean b) {
          mParentOnly = b;
     public void setModal(boolean b) {
          super.setModal(b);
          //mModal = b;
          doModalOp();
     private void doModalOp() {
          if (mOwner != null) {
               mOwner.setEnabled(!super.isModal() || !isVisible());
     public boolean isModal() {
          return super.isModal() && !mParentOnly;
//     public boolean isParentModel() {
//          return mModal;
     public void windowOpened(WindowEvent arg0) {
          doModalOp();
     public void dispose() {
          //setVisible(false);
          //doModalOp();
          if (mOwner != null) {
               mOwner.setEnabled(true);
          removeComponentListener(this);
          removeWindowListener(this);
          super.dispose();
     public void pack() {
          super.pack();
          addListeners();
     private boolean addListeners() {
          WindowListener[] listeners = this.getWindowListeners();
          if (listeners != null) {
               for (int i = 0; i < listeners.length; i++) {
                    if (listeners[i] == this) return false;
          addComponentListener(this);
          addWindowListener(this);
          return true;
     public void show() {
          super.show();
          addListeners();
     public void show(boolean b) {
          super.show(b);
          addListeners();
     public void windowClosing(WindowEvent arg0) {}
     public void windowClosed(WindowEvent arg0) {
          doModalOp();
     public void windowIconified(WindowEvent arg0) {}
     public void windowDeiconified(WindowEvent arg0) {}
     public void windowActivated(WindowEvent arg0) {}
     public void windowDeactivated(WindowEvent arg0) {}
     public static void main(String[] args) {
          JFrame jf1 = new JFrame("1"), jf2 = new JFrame("2");
          final ModalOnParentDialog d = new ModalOnParentDialog(jf1, "3");
          d.setModal(true);
          d.setParentOnly(true);
          JButton b = new JButton("Show");
          b.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent ae) {
                    d.setVisible(!d.isVisible());
          jf1.setBounds(0,0,40,40);
          jf1.setDefaultCloseOperation(jf1.DISPOSE_ON_CLOSE);
          jf2.setDefaultCloseOperation(jf2.DISPOSE_ON_CLOSE);
          d.setDefaultCloseOperation(d.DISPOSE_ON_CLOSE);
          jf2.setBounds(40,40,40,40);
          jf2.add(b);
          d.setBounds(80,80,40,40);
          jf1.setVisible(true);
          jf2.setVisible(true);
          d.setVisible(true);
}

Similar Messages

  • How can you get CMD + L to open a new window, when all windows are closed?

    In Firefox 28 and earlier, you could hit Apple + L (CMD + L) when no FF windows were open, and it would pull up a blank page with the cursor blinking in the address bar.
    After installing FF 29, when you hit CMD + L (with no windows open) it just plays the system alert sound and does nothing.
    I want to be able to hit CMD + TAB to gain control of an idle Firefox, and then be able to hit CMD + L to gain control of a BLANK address bar.
    Every other version of Firefox I've ever used did this, so am I missing something here? Any ideas?
    Note: I realize that you can hit CMD + N to launch a new window and then CMD + L to select the text within the address bar. I'm not looking to re-train my habits within Firefox, but simply want that feature like it was in the previous 28 versions.

    Well, after an entire week without a word from Mozilla, I officially give up.
    I understand that new software will have bugs when it is released. I understand that the bugs are fixed in the order of greatest impact/priority. I also understand that my issue is not a bug, rather a feature that was inexplicably removed.
    A couple of things I cannot understand —
    Why would Mozilla remove '''ANY''' features from their software when they are touting this as the most customizable web browser ever? That is obviously not logical and will be to their detriment in the long run.
    Furthermore, if a person did get on here just to complain about this update, they were immediately met with a canned response from a moderator, which offered a few options to make Firefox '''LOOK''' like the previous version. That speaks volumes about this update and the developers' confidence in it. The person who gets on here with a legitimate problem asking why a feature was removed — they get '''NO''' response from a moderator. Again, this speaks volumes. I am not even asking for an immediate solution to this problem, just the acknowledgement that there is a problem. On top of that, especially in the development of software, the old adage particularly rings its truest — ''If it ain't broke, don't fix it.''
    Lastly, I think we can all agree that Firefox 29 is a blatant ripoff of Google Chrome. If I wanted Google Chrome, that is exactly what I would have downloaded. That pains me not only as a devout Firefox user for nearly 10 years, but especially as I have donated over $200 to the Mozilla Foundation in that time. In other words, I have financially supported the development of a software that has become absolutely indifferent to its users' needs, and moreover, they have now resorted to copying off of their neighbor.
    I have since rolled back versions — all the way to Netscape Navigator 9 — which seems to have just about as much functionality as the bungled mess that is Firefox 29.
    I genuinely appreciate all of the time and effort that goes in to creating open-source software, and I financially support the projects that I feel adhere to the open-source philosophy. That said, Mozilla.org seems to be straying away from this philosophy, to the point where, for me, it is simply time for a change.
    Thanks for the memories, Firefox!

  • How can I shut down when printer window is locked?

    How can I proceed to shut down when printer window is locked?

    Thank you, i will try this option. Only thing is i did read another person having this problem and he did shut down his computer and now his photo library in unaccessible!!! Ugh. I do have an external back up hard drive. I sure hope that my photos are on there to reload into iPhoto!!!!
    I did try to place my mouse over the iPhoto icon @ the dock and held down the control key with no results in shutting down the application.

  • My menu bar does not appear at the top of my homepage on a brand new 27" iMac only when i pull up an app. this does not allow me to shut down my mac when all apps are closed nor does it all me to use finder. any help would be awesome

    My menu bar does not appear at the top of my homepage on a brand new 27" iMac only when i pull up an app. this does not allow me to shut down my mac when all apps are closed nor does it all me to use finder. any help would be awesome

    Press the escape (esc) key to exit full-screen mode.

  • Computer doesn't shut down when PXI Chasis is turned off

    Hello,
    I have realized that my computer doesn't shut down propertly if I turn off my PXI-1036 chasis before the computer is turned off. Is this a normal behaivour?
    As far as I remember, the PXI-1036 chasis must be turned on before the computer is power on, and I think that maybe on the turning off I have to follow the same procedure.
    Thank you very much for your advice,
    Héctor
    Solved!
    Go to Solution.

    Yeah, Robert's correct and I was backwards.  I haven't used my 1033 chassis for years.  Guess I should have tried it before answering.
    Using LabVIEW: 7.1.1, 8.5.1 & 2013

  • Why does my macbook shut down when the lid is closed?

    Hi,
    I have a Macbook Pro (mid-2012) running 10.9.1
    Last week out of the blue, the Macbook started shutting down whenever I close the lid.
    When I open the lid, the logon screen appears (as if it's been asleep as normal) but almost immediately reboots itself.
    Has anyone else got the same issue? Any idea how to resolve?
    Thanks

    If after you open the lid you do get the login screen, almost immediately, but then it restarts your system is not shutting down when you close the lid. It is going into a sleep state but then crashes when trying to come out of that sleep state. There are quite a few posts on this problem and it seems to be caused by the system going into the Deep Sleep state also known as the hibernate mode. That is where the contents of the RAM is written to the hard drive and the system suspends supplying power to the RAM chips so the system uses even less power then in regular sleep mode.
    That is only supposed to happen after the system has been in the regular sleep mode for an extended period of time.
    To check the state of the huiberbnate mode open terminal and type in, or copy and paste, this command.
    pmset -g | grep hibernatemode
    To check the state of all power management settings use the command.
    pmset -g

  • MacBook Pro keeps shutting down when the screen is closed

    Hi, I have a 2011 15inch MacBook Pro and it keeps shutting down rather than sleeping when I shut the screen. It also shuts down when left for short periods of time rather than sleeping. I have updated it yesterday and still the problem happens. I also installed a virus check to see if that was the problem and it didnt find anything it could fix! Any help is much appreciated!!!

    Try a SMC reset.  That may correct the situation.
    http://support.apple.com/en-us/HT201295
    Ciao.

  • Xorg shutting down when certain keys are pressed

    Hello after some recent updates, I've experienced that xserver is shutting down and I need to restart my computer to get a working a system back.
    When I hold keys like backspace arrow keys this happens, anyone experienced the same lately?

    (II) config/hal: Adding input device Logitech Logitech BT Mini-Receiver
    (EE) config/hal: NewInputDeviceRequest failed (8)
    (II) config/hal: Adding input device Logitech Logitech BT Mini-Receiver
    (EE) config/hal: NewInputDeviceRequest failed (8)
    (II) config/hal: Adding input device G15 Keyboard G15 Keyboard
    (EE) config/hal: NewInputDeviceRequest failed (8)
    (II) config/hal: Adding input device Gaming Keyboard
    (EE) config/hal: NewInputDeviceRequest failed (8)
    (II) config/hal: Adding input device Gaming Keyboard
    (EE) config/hal: NewInputDeviceRequest failed (8)
    (II) config/hal: Adding input device Logitech USB Gaming Mouse
    (EE) config/hal: NewInputDeviceRequest failed (8)
    (II) config/hal: Adding input device Macintosh mouse button emulation
    (EE) config/hal: NewInputDeviceRequest failed (8)
    I have disabled the hotplugging in my config.

  • When all apps are closed the text vanishes from my toolbar.  How do i get it back?

    When all the apps are closed the text vanishes from the tool bar at the top of the screen.  The apple icon is still there on the far left but clicking on it does nothing.  I can't log out!!!!

    How do I find that out? I just switched to Apple and am finding it really hard to find stuff.

  • [nForce] K7N2GM2 Shutting down when installing windows

    I have tried everything. I have changed the processor, I have changed the memory, I have changed the hard disk but the same thing happens everytime. The machine boots fine. I go into the bios and I can see everything. I even manage to format a sata drive. Then when it starts to install windows it simply hangs. I then tried changing the settings to the safe settings in the bios and now it gets starting windows within the installation and crashes. I can't power it up again without pulling the power cord out the back.
    The only thing I can think of is that the power supply on my micro ATX case is not poweful enough.
    Any help will be greatly appreciated,
    Jim.

    http://www.nvidia.com/content/drivers/drivers.asp
    look under Platform Drivers, and download the latest Nforce drivers, or just the new Audio driver
    does the audio device show in Device Manager? is it enabled in BIOS?
    BTW why do you have a Geforce 4 MX400 VGA card? the K7N2GM board has integrated Geforce 4 MX440 graphics?

  • The iPad shuts down when the cover is closed so I can't get mail message or Skype alerts?

    Didnt do it at first but now when the ipad cover is closed it wont alert me to mail or massages etc.
    Done everything I can think of but to no avail.
    Perhaps I have changed a setting without realising it...
    Can any one help with that one?

    I have tried everything I could to fix this, but some things require actually being on Firefox, and since I cannot get on, I cannot click on the tabs to do it. I have even totally uninstalled firefox, and that has not fixed this. I still get the same message that firefox is running and I need to close it or restart (which I have also tried dozens of times). I have removed things like Java, and that has not helped either. If I cannot even get on line in firefox, how can I fix this. I am not crazy about using internet explorer, but right now, it is my only option. I even tried to start in safe mode, and the same message box pops up!

  • How come everytime my Mac shuts down, when I turn it back on ,Garageband has to Re-Download the program files?

    How come every time my Mac shuts down,when Iturn it back on,Garageband has to Re-Download the program files?

    Check this article http://support.apple.com/kb/HT1547 and this one http://support.apple.com/kb/HT2341?viewlocale=en_US
    If it sounds like a RAM issue it could be one of the RAM chips has failed. You could try removing the RAM one at a time and see if it starts with one still installed.

  • Macbook Doesn't Shut Down

    My Macbook doesn't shut down when I use it for longer than 4-5 hours. Even if there are no programs running it still doesn't shutdown, the button may as well have a question mark on it because it does nothing.
    Little help please. It's getting annoying having to hold the power button to shut it down.
    (Restart and Logout don't work either but Standby does)

    This same problem is happening to me also. Doesn't happen all the time but enough to be a problem. I will use the shut down menu option and then the desktop icons leave; however, the dock is still there. I have waited several minutes (over 10) to see if the computer will shut down. I finally have to push and hold the power button and then the computer shuts down. I have not been able to find a common issue for this--appears random.

  • OS on external firewire doesn't shut down

    Hi. I have an OS for emergencies on an external firewire drive. So after updating to OSX 10.5.8 my external firewire sometimes doesn't shut down when I used it as the bootdrive, and want to switch to my internal harddrive for booting (I of course select the start up disk in system preferences before shut down or restart). On both harddrives are 10.5.8 installed. So I don't know. I have done disk verify (and repair once on the external as needed) and I have done permission repair on both pretty often. Switching from the internal to the external firewire is no problem, but switching back sometimes is. What happens is when the external shuts down, after all apps are quit (by me of course), finder, dock and all desktop icons disappear, only the mouse pointer and desktop background pic stays, and nothing else happens. I never had this problem on Tiger. I suspect a conflict between the same OSs on both drives. If I can get the firewire to shut down correctly, I am considering to always hold down the option key at start up and there select which drive to boot from, instead of doing it before in system preferences of either running OS. Any thoughts are appreciated!!

    Yeah. Thanks. But I also have the same problem with my internal HD. Freezes before log out sometimes. If I reach log out, then no problem to shut down or restart. Very strange....and in fact painful turning the computer off sometimes having to press the power button 10 secs.

  • 10.9.5 my iMac shuts down when supposedly asleep

    How can I stop my iMac shutting down when it is programmed to sleep.   10.9.5

    How can I stop my iMac shutting down when it is programmed to sleep.   10.9.5

Maybe you are looking for