Solved

windowClosing method from WindowAdapter not working

Posted on 2002-04-18
5
415 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
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error trying to install RTMT Win7 5 60
fibonacci ten numbers 4 39
How do I remove an object from a 3 26
login form jsp example 2 26
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

810 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