Solved

windowClosing method from WindowAdapter not working

Posted on 2002-04-18
5
413 Views
Last Modified: 2013-11-23
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
Comment
Question by:baileysl
  • 3
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
Comment Utility
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
 

Expert Comment

by:cini_gm
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
Didn't I just say exactly that??
But nice work with the cut and paste.
0
 

Author Comment

by:baileysl
Comment Utility
Thanks!  I did have to move the WindowCloser Class from inside of my other listener class.

Sheila
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Happy to help :-)

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

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
basic hardware to learn oop advanced design patterns 3 71
json example 39 110
maven project error 5 16
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now