Create Frame Window With Window Close Event Example

  1. /*
  2. Create Frame Window With Window Close Event Example
  3. This java example shows how to create frame window and handle windowClosing
  4. event using WindowAdapter class.
  5. */
  6.  
  7. import java.awt.Frame;
  8. import java.awt.event.WindowAdapter;
  9. import java.awt.event.WindowEvent;
  10.  
  11. /*
  12. * To create a stand alone window, class should be extended from
  13. * Frame and not from Applet class.
  14. */
  15.  
  16. public class CreateWindowWithEventsExample extends Frame{
  17.  
  18. CreateWindowWithEventsExample(String title){
  19. //call the superclass constructor with the specified title
  20. super(title);
  21.  
  22. //add window event adapter
  23. addWindowListener(new MyWindowAdapter(this));
  24.  
  25. //set window size using setSize method
  26. this.setSize(300,300);
  27.  
  28. //show window using setVisible method
  29. this.setVisible(true);
  30. }
  31.  
  32. //extend WindowAdapter
  33. class MyWindowAdapter extends WindowAdapter{
  34.  
  35. CreateWindowWithEventsExample myWindow = null;
  36.  
  37. MyWindowAdapter(CreateWindowWithEventsExample myWindow){
  38. this.myWindow = myWindow;
  39. }
  40.  
  41. //implement windowClosing method
  42. public void windowClosing(WindowEvent e) {
  43. //hide the window when window's close button is clicked
  44. myWindow.setVisible(false);
  45. }
  46. }
  47.  
  48. public static void main(String[] args) {
  49. CreateWindowWithEventsExample myWindow =
  50. new CreateWindowWithEventsExample("Window with event Example");
  51. }
  52. }

Example Output

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!