Solved

Why won't this application close?

Posted on 2003-11-15
7
278 Views
Last Modified: 2010-03-31
Hi All,

This error may be the result of poor construction but when I click the title
 bar box X to close this window will not close?

And when I try uncommenting the processWindowEvent at the
bottom does not seem to work either. Infact it is even worse. I have to go to
 the Win2k task Mgr (ctrl, alt, delete)  to shut it off

Help appreciated.

Delphi3



package systeminouttotxtfieldinout;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class framedTextFieldInOut {
  public framedTextFieldInOut() {
    boolean packFrame = false;
    //Construct the application
    Frame1 frame = new Frame1();
    if (packFrame) {
      frame.pack();
    }
    else {
      frame.validate();
    }
    //Center the window
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height) {
      frameSize.height = screenSize.height;
    }
    if (frameSize.width > screenSize.width) {
      frameSize.width = screenSize.width;
    }
    frame.setLocation( (screenSize.width - frameSize.width) / 2,
                      (screenSize.height - frameSize.height) / 2);
    frame.setTitle("The Conversions of Fahrenheit and Celsius ");
    frame.setVisible(true);

  }

  //Main method
  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    new framedTextFieldInOut();
  }

  public class Frame1
      extends Frame {
    //Fahrenheit,Celsius
    float Fahr, Cels;
    Label f = new Label("Input F  Press Return Conversion below ");
    Label c = new Label("Input C Press Return Conversion above ");
    TextField tf1 = new TextField(5);
    TextField tf2 = new TextField(5);

    Frame1() {
      setSize(400, 100);
      setLayout(new FlowLayout());
      add(f);
      tf1.addActionListener(new Fahren());
      add(tf1);
      add(c);
      tf2.addActionListener(new Celsi());
      add(tf2);

    }

    class Fahren
        implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        Fahr = Float.valueOf(tf1.getText()).floatValue();
        Cels = (float) ( (Fahr - 32) / 1.8);
        tf2.setText(Float.toString(Cels));
      }
    }

    class Celsi
        implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        Cels = Float.valueOf(tf2.getText()).floatValue();
        Fahr = (float) (1.8 * Cels) + 32;
        tf1.setText(Float.toString(Fahr));
      }
    }

    //Overridden so we can exit when window is closed
    // protected void processWindowEvent(WindowEvent e) {
    //   super.processWindowEvent(e);
    //   if (e.getID() == WindowEvent.WINDOW_CLOSING) {
    //     System.exit(0);

    //   }
    //  }

  }
}
0
Comment
Question by:delphi3
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 9754597
The method

   protected void processWindowEvent(WindowEvent e)

should be

   public void processWindowEvent(WindowEvent e)

Try that and see how it works out.

\t
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9754905
>>UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Is not appropriate in a non-Swing application and is anyway redundant
0
 
LVL 4

Author Comment

by:delphi3
ID: 9755954
Hi
Sorry but I tried both suggestions and neither of them make a difference.

So any other bright ideas?

Thanks Delphi3
0
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.

 
LVL 86

Expert Comment

by:CEHJ
ID: 9755991
Make this the first line of your frame constructor:

addWindowListener(this);
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 9756940
Add following to your ctor:

enableEvents(AWTEvent.WINDOW_EVENT_MASK);
0
 
LVL 4

Author Comment

by:delphi3
ID: 9768305
Hi All,
Considering the suggestions that were offered objects' solution offers the one that allows the window to close
when I placed his line in the section of the frame and then  uncommented the lines previously written in the program.

....... copied from above with changes in place .........

   public class Frame1
      extends Frame {
    //Fahrenheit,Celsius
    float Fahr, Cels;
    Label f = new Label("Input F  Press Return Conversion below ");
    Label c = new Label("Input C Press Return Conversion above ");
    TextField tf1 = new TextField(5);
    TextField tf2 = new TextField(5);

    Frame1() {
      enableEvents(AWTEvent.WINDOW_EVENT_MASK);// added from EE objects
      setSize(400, 100);
      setLayout(new FlowLayout());
      add(f);
      tf1.addActionListener(new Fahren());
      add(tf1);
      add(c);
      tf2.addActionListener(new Celsi());
      add(tf2);

    }

    class Fahren
        implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        Fahr = Float.valueOf(tf1.getText()).floatValue();
        Cels = (float) ( (Fahr - 32) / 1.8);
        tf2.setText(Float.toString(Cels));
      }
    }

    class Celsi
        implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        Cels = Float.valueOf(tf2.getText()).floatValue();
        Fahr = (float) (1.8 * Cels) + 32;
        tf1.setText(Float.toString(Fahr));
      }
    }

    //Overridden so we can exit when window is closed
    protected void processWindowEvent(WindowEvent e) {
      super.processWindowEvent(e);
      if (e.getID() == WindowEvent.WINDOW_CLOSING) {
        System.exit(0);

      }
    }

  }
}
0
 
LVL 92

Expert Comment

by:objects
ID: 9768334
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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
HashTable highest marks enumeration alternative 9 43
Error in @AspectJ Based AOP with Spring 2 18
Unhandled exception type Exception 18 42
Java basic valueOf question 1 29
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

829 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