Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Failing to properly Close Windows

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Failing to properly Close Windows

    On my journey through my java Book, I'm starting to learn about frames.

    When you click the "X" to close the window, this program does not completely terminate for some reason, you have to use ctrl^c in the console to exit it. I do not understand why, unless this approach is deprecated. Please Help.

    WindowClosingTest.java
    // WindowClosingTest.java
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class WindowClosingTest extends JFrame
    {
      JTextArea frameTextArea;
      Container frameContainer;
     
      public WindowClosingTest()
      {
        super("WindowClosingTest Application");
        init();
      }
     
      public void init()
      {
        addWindowListener(new WindowCloser());
     
        frameContainer = getContentPane();
        frameTextArea = new JTextArea("\t Window Closing Test Class.", 50, 50);
     
        frameTextArea.setEditable(true);
        frameTextArea.setLineWrap(true);
     
        frameContainer.add(frameTextArea);
     
        setSize(520, 300);
        setVisible(true);
     
      }
     
      public static void main(String Args[])
      {
        new WindowClosingTest();
      }
     
    }//end class

    WindowCloser.java
    //WindowCloser.java
     
    import java.awt.event.*;
    import javax.swing.*;
     
    public class WindowCloser extends WindowAdapter
    {
      public WindowCloser(){}
     
      public void windowClosing(WindowEvent wE)
      {
        System.exit(0);
      }
    }//end class
    Last edited by Spidey1980; August 23rd, 2011 at 02:11 PM.


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    86
    My Mood
    Lurking
    Thanks
    16
    Thanked 4 Times in 4 Posts

    Default Re: Failing to properly Close Windows

    nevermind I got it, (WindowEvent wE) should be (WindowEvent e). Why the the book had it as "wE" and not "e" i'll never know.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Failing to properly Close Windows

    a) wE or e, it should not matter as long as when you access the variable inside the method you use the appropriate name b) Read the API for JFrame, you are better off using setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Failing to properly Close Windows

    A potential problem when you extend an adapter class is that if you misspell the method you want to override, the compiler is very happy to add that method as a new one vs overriding another.
    Use the @Override statement just before your method definition to have the compiler check if your method is an override.

Similar Threads

  1. Replies: 1
    Last Post: February 10th, 2012, 10:05 AM
  2. Simple TCP utility failing right out the gate
    By Blackbird in forum Java Networking
    Replies: 2
    Last Post: July 8th, 2011, 08:30 AM
  3. Is this correct.Am I close? Help please
    By eagle09 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 26th, 2011, 07:26 AM
  4. Trying to add a close button
    By coyboss in forum Java Theory & Questions
    Replies: 5
    Last Post: February 12th, 2011, 03:28 PM
  5. [SOLVED] Trying to call web service (and failing)
    By jamesruk21 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 15th, 2011, 12:35 PM