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
Solved

windowClosing method from WindowAdapter not working

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
table example 4 32
MySqlDump not dumping triggers 1 43
servlet example 17 51
difference between sorce folder and folder in eclipise 3 28
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

856 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