Solved

windowClosing method from WindowAdapter not working

Posted on 2002-04-18
5
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 6952071
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
ID: 6952591
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
ID: 6952598
Didn't I just say exactly that??
But nice work with the cut and paste.
0
 

Author Comment

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

Sheila
0
 
LVL 92

Expert Comment

by:objects
ID: 6955136
Happy to help :-)

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

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

623 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