Article View: borland.public.jbuilder.java.api
Article #937Re: an idea to consider
From: Alan Shiers
Date: Sun, 07 Jul 2002 20:18
Date: Sun, 07 Jul 2002 20:18
177 lines
5588 bytes
5588 bytes
Hi there,
Ok, I finally found some stuff on Runtime.addShutdownHook(Thread t).
I put together a simple test application with the classes you see below.
However, I don't think the behavior is what I'm looking for. The
documentation says, "When the virtual machine begins its shutdown
sequence it will start all registered shutdown hooks..." This tells me
it is waiting itself for a signal to shut the program down. How does it
know when CTRL+C has been pressed?
What I really want is a threaded class that is constantly monitoring for
some kind of signal to stop the application. This threaded class that
gets passed to the method addShutdownHook(Thread t) only gets executed
when I manually close the JFrame object using the close button in the
upper right hand corner of the frame. This is evident by the sequence
of println() statements that I've encorporated in the test application.
While it was running I tried pressing CTRL+C on the keyboard, but
nothing happens! I was thinking that the Runtime class was monitoring
for this activity. I guess not...
What am I missing in my code?
Alan
***********************************************************
package shutdownhooktest;
import javax.swing.UIManager;
import java.awt.*;
public class Application1
{
boolean packFrame = false;
ShutdownHook hook = null;
/**Construct the application*/
public Application1()
{
hook = new ShutdownHook(this);
Runtime.getRuntime().addShutdownHook(hook);
Frame1 frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their
layout
if (packFrame)
{
frame.pack();
}
else
{
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
public void shutdown()
{
System.out.println("executing shutdown sequence");
System.exit(0);
}
/**Main method*/
public static void main(String[] args)
{
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
e.printStackTrace();
}
new Application1();
}
}
***********************************************************
package shutdownhooktest;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame1 extends JFrame
{
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea jTextArea1 = new JTextArea();
/**Construct the frame*/
public Frame1()
{
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception
{
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your
Icon]")));
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
jTextArea1.setText("jTextArea1");
contentPane.add(jScrollPane1, BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTextArea1, null);
}
/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e)
{
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
System.out.println("processing Window Closing event.");
System.exit(0);
}
}
}
********************************************************
package shutdownhooktest;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2000
* Company:
* @author
* @version 1.0
*/
public final class ShutdownHook extends Thread
{
Application1 main = null;
public ShutdownHook(Object mainClass)
{
main = (Application1)mainClass;
}
public void run()
{
System.out.println("Shutting down application");
main.shutdown();
}
}
Technical Details
Message-ID:
<3D28A22A.89CB73DC@hfx.eastlink.ca>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-05!sn-xit-06!supernews.com!iad-peer.news.verio.net!news.verio.net!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnewsReferences:
<3D279D25.59AA52E@hfx.eastlink.ca> <3d27a0b2_2@dnews> <3D2861BB.63220DF5@hfx.eastlink.ca> <MPG.17923b35889aef9498b7f9@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1089d0,4157c064c52214d4 X-Google-Attributes: gid1089d0,public X-Google-ArrivalTime: 2002-07-07 12:31:27 PST Message-ID: <3D28A22A.89CB73DC@hfx.eastlink.ca> Date: Sun, 07 Jul 2002 16:18:50 -0400 From: Alan Shiers <ashiers@hfx.eastlink.ca> Organization: Mustard Seed Software X-Mailer: Mozilla 4.78 [en] (Win98; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.api Subject: Re: an idea to consider References: <3D279D25.59AA52E@hfx.eastlink.ca> <3d27a0b2_2@dnews> <3D2861BB.63220DF5@hfx.eastlink.ca> <MPG.17923b35889aef9498b7f9@newsgroups.borland.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.222.101.2 X-Trace: dnews 1026069379 24.222.101.2 (7 Jul 2002 12:16:19 -0700) Lines: 177 Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-05!sn-xit-06!supernews.com!iad-peer.news.verio.net!news.verio.net!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!dnews