Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

windowClosing method from WindowAdapter not working

I am new to Java.  I a learning using the programming problems from the book "Java Programming From the Beginning".  I am at the point of learning about AWT.  I am trying to use the WindowAdapter class to close my frame when I click on the "X" in the upper right-hand conner of my frame.  Everything compiles, but the windowClosing method does not work.  What is wrong.  I am using JDK 1.3.1_01.  Here is my source code.

/*
 * Rot13.java
 *
 * Created on March 27, 2002, 6:06 PM
 */

/**
 *
 * @author  sbailey
 * @version
 */

import java.awt.*;
import java.awt.event.*;

class Rot13Frame extends Frame {
   private TextField originalMessage = new TextField();
   private TextField encryptedMessage = new TextField();
   
   // Constructor
   public Rot13Frame(String title) {
      // Set title for frame and choose layout
      super(title);
      setLayout(new GridLayout(2,2));
     
      // Add Original Message label and text field to frame; attach
      // listener to text field
      add(new Label("Original Message"));
      add(originalMessage);
      originalMessage.addActionListener(new MessageListener());
     
      //Add Encrypted Message label and text field to frame
      add(new Label("Encrypted Message"));
      add(encryptedMessage);
      encryptedMessage.setEditable(false);
   }
   
   // Listener for originalMessage
   private class MessageListener implements ActionListener {
      public void actionPerformed(ActionEvent evt) {
         String originalMessageString = originalMessage.getText();
         StringBuffer encryptedMessageString =
               new StringBuffer(originalMessageString.length());
         for (int i = 0; i < originalMessageString.length(); i++)
          encryptedMessageString.append(encrypt(originalMessageString.charAt(i)));
         encryptedMessage.setText(encryptedMessageString.toString());
      }
     
      //Listener for window
      private class WindowCloser extends WindowAdapter {
         public void windowClosing(WindowEvent evt) {
            System.exit(0);
         }
      }
     
      // Returns the encryted version of ch using the "rot13 algorithm
      private char encrypt(char ch) {
         if (!Character.isLetter(ch))
             return ch;
         if (Character.isUpperCase(ch))
             return (char) (((ch - 'A') + 13) % 26 + 'A');
         return (char) (((ch - 'a') + 13) % 26 + 'a');
      }
   }
}

public class Rot13 {

   /** Creates new Rot13 */
    public Rot13() {
    }

    /**
    * @param args the command line arguments
    */
    public static void main (String args[]) {
       Frame myFrame = new Rot13Frame("Rot13");
       myFrame.setSize(300,75);
       myFrame.setVisible(true);
    }
   
}
0
baileysl
Asked:
baileysl
  • 3
1 Solution
 
objectsCommented:
You have not added a window listener to your frame. You need to do this so that it is called when the window closes. Add the following inside your ctor:

addWindowListener(new WindowCloser());
0
 
cini_gmCommented:
Here I am giving the modified code, pls check...
I added one method in the constructor
 this.addWindowListener(new WindowCloser());
Also I corrected the } in the MessageListener class.


import java.awt.*;
import java.awt.event.*;

class Rot13Frame extends Frame {
  private TextField originalMessage = new TextField();
  private TextField encryptedMessage = new TextField();
 
  // Constructor
  public Rot13Frame(String title) {
     // Set title for frame and choose layout
     super(title);
     setLayout(new GridLayout(2,2));
     
     // Add Original Message label and text field to frame; attach
     // listener to text field
     add(new Label("Original Message"));
     add(originalMessage);
     originalMessage.addActionListener(new MessageListener());
     
     //Add Encrypted Message label and text field to frame
     add(new Label("Encrypted Message"));
     add(encryptedMessage);
     encryptedMessage.setEditable(false);
     this.addWindowListener(new WindowCloser());
  }
 
  // Listener for originalMessage
  private class MessageListener implements ActionListener {
     public void actionPerformed(ActionEvent evt) {
        String originalMessageString = originalMessage.getText();
        StringBuffer encryptedMessageString =
              new StringBuffer(originalMessageString.length());
        for (int i = 0; i < originalMessageString.length(); i++)
         encryptedMessageString.append(encrypt(originalMessageString.charAt(i)));
        encryptedMessage.setText(encryptedMessageString.toString());
     }
  }  
     //Listener for window
     private class WindowCloser extends WindowAdapter {
        public void windowClosing(WindowEvent evt) {
           System.exit(0);
        }
     }
     
     // Returns the encryted version of ch using the "rot13 algorithm
     private char encrypt(char ch) {
        if (!Character.isLetter(ch))
            return ch;
        if (Character.isUpperCase(ch))
            return (char) (((ch - 'A') + 13) % 26 + 'A');
        return (char) (((ch - 'a') + 13) % 26 + 'a');
     }
 }


public class Rot13 {

  /** Creates new Rot13 */
   public Rot13() {
   }

   /**
   * @param args the command line arguments
   */
   public static void main (String args[]) {
      Frame myFrame = new Rot13Frame("Rot13");
      myFrame.setSize(300,75);
      myFrame.setVisible(true);
   }
   
}
0
 
objectsCommented:
Didn't I just say exactly that??
But nice work with the cut and paste.
0
 
baileyslAuthor Commented:
Thanks!  I did have to move the WindowCloser Class from inside of my other listener class.

Sheila
0
 
objectsCommented:
Happy to help :-)

http://www.objects.com.au
Brainbench MVP for Java 1
http://www.brainbench.com
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now