Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1516
  • Last Modified:

Applet - window popup message

Hello,

I was speaking with someone and they said the following code will give a deprecated error.

    public boolean handleEvent(Event event) {
        if(event.id == Event.WINDOW_DESTROY) {
            super.dispose();
        } // end if
        return true;
    } // end handleEvent

Questions:

1.  What does deprecated mean?
2.  I compiled and ran the code, and it seems to work fine!  Will the above code throw a deprecated error?

Thank you!

-luna621 =^^=

---------------------------------------------------------------------------------------------------------------------------------------------

import java.awt.*;
import java.io.*;
import java.awt.event.*;

class PF extends Frame {

    private Button close = new Button("Close the window");

    public PF(String smsg) {
        setLayout(new FlowLayout());
        setBackground(Color.red);
        Label l = new Label("This frame is up because you clicked " + smsg);
        ButtonHandler handler = new ButtonHandler();
        close.addActionListener(handler);
        add(l);
        add(close);
    } // end constructor


    public boolean handleEvent(Event event) {
        if(event.id == Event.WINDOW_DESTROY) {
            super.dispose();
        } // end if
        return true;
    } // end handleEvent


    private class ButtonHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            Button b = (Button)e.getSource();
            if(b.equals(close)) {
                dispose();
            } // end if
        } // end actionPerformed
    } // END ButtonHandler class

} // END PF class
0
luna621
Asked:
luna621
  • 10
  • 7
  • 6
2 Solutions
 
luna621Author Commented:
Oh, here's the code with the init():

---------------------------------------------------------------------------------------------------------------------------------------------

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;


public class BA extends Applet {

    private Button pop1 = new Button("pop Message 1");
    private Button pop2 = new Button("pop Message 2");
    private Button pop3 = new Button("pop Message 3");
    private AppHandler ears = new AppHandler();

    public void init() {

        pop1.addActionListener(ears);
        pop2.addActionListener(ears);
        pop3.addActionListener(ears);

        add(pop1);
        add(pop2);
        add(pop3);

    } // end init()


    private class AppHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String s = e.getActionCommand();
            if((s.equals("pop Message 1"))||(s.equals("pop Message 2"))||(s.equals("pop Message 3"))) {
                PF pf = new PF(s);
                pf.setSize(500,200);
                pf.setTitle(s);
                pf.show();
            } // end if
        } // end actionPerformed
    } // END AppHandler class
} // END BA class
0
 
objectsCommented:
> 1.  What does deprecated mean?

Means the method may not work or be removed entirely in a future release of Java.

> 2.  I compiled and ran the code, and it seems to work fine!  Will the above code throw a deprecated error?

yes

0
 
luna621Author Commented:
Ooo... that's bad.  Is there a way to fix it?
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
objectsCommented:
depends on what versions of Java you want to support.
0
 
JavatmCommented:
> 1.  What does deprecated mean?

Deprication error is a way or method which is not acceptable on how you use it.
You can test it by compiling with an option like :

javac -deprecation YourProgram.java

> 2.  I compiled and ran the code, and it seems to work fine!  Will the above code throw a deprecated error?

Yes !, If you want to see it your self then use the compilation example above.

Hope that helps . . .
Javatm
0
 
JavatmCommented:
> Ooo... that's bad.  Is there a way to fix it?

There's a way if you could change the way you handle the method or
on the Java version you have.
0
 
luna621Author Commented:
Well, I have Java 1.4.2 right now.  So if java were to update again, my code will not work.

-------------------------------------------------------------------------------------------------------------------------------------------
Comment from Javatm
Date: 04/30/2004 05:33PM HST

There's a way if you could change the way you handle the method or
on the Java version you have.
-------------------------------------------------------------------------------------------------------------------------------------------

Do you mean this method:

public boolean handleEvent(Event event) {
...
}
0
 
JavatmCommented:

> Do you mean this method:
> public boolean handleEvent(Event event) {
> ...
>}

Yup, It depends actually on the program its self. I had encountered a lot of that before
so what like in JDK 1.5 instead of JFrame.show(); its already JFrame.setVisible(true);
So it depends on your program you need to check it and run it yourself.
0
 
luna621Author Commented:
Okay, here's the message it gave me:

The method boolean handleEvent(java.awt.Event) declared in class PF is not deprecated, but overrides a deprecated method of the same signature declared in class java.awt.Component.
    public boolean handleEvent(Event event) {
                   ^
Note: PF.java uses or overrides a deprecated API.  Please consult the documentation for a better alternative.
0
 
objectsCommented:
> Well, I have Java 1.4.2 right now.  So if java were to update again, my code will not work.

No the problem is if someone needs to run your code with an earlier java version.

if you just want to run under 1.4.2 then just remove the entire method.
0
 
objectsCommented:
or you could replace it with the processEvent() method.
0
 
JavatmCommented:
Try using a different approach !.
0
 
luna621Author Commented:
Comment from objects
Date: 04/30/2004 05:44PM HST

or you could replace it with the processEvent() method.
--------------------------------------------------------------------------------------------

The processEvent() method??
 
0
 
JavatmCommented:
Or you could change your extends Frame to extends JFrame and process the
window closing like :

YourFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);

or you can do it like :

  // Main Method

  public static void main( String args[] )
   {
      PF app = new PF();

      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent e )
            {
               System.exit(0);
            }
         }
      );
   }
0
 
objectsCommented:
> The processEvent() method??

thats the method that replaced handleEvent() in Java 1.1.
0
 
luna621Author Commented:
Probably won't use this one, since it will be running in an applet... needs the init(), right?

  public static void main( String args[] )
   {
      PF app = new PF();

      app.addWindowListener(
         new WindowAdapter() {
            public void windowClosing( WindowEvent e )
            {
               System.exit(0);
            }
         }
      );
   }
--------------------------------------------------------------------------

Still trying to get this to work: YourFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);

I'm getting errors:

cannot resolve symbol
symbol  : class JFrame
class PF extends JFrame {
                           ^
cannot resolve symbol
symbol  : method setLayout (java.awt.FlowLayout)
        setLayout(new FlowLayout());
        setBackground(Color.red);
        add(l);
        add(close);
        super.dispose();
        dispose();

7 errors



Do I need to import something??
0
 
luna621Author Commented:
Comment from objects
Date: 04/30/2004 06:00PM HST

> The processEvent() method??

thats the method that replaced handleEvent() in Java 1.1.
---------------------------------------------------------------------------------------------------------------

So instead of handleEvent, I should write processEvent?
0
 
luna621Author Commented:
Hmm... that seems to have worked!


    public boolean processEvent(Event event) {
        if(event.id == Event.WINDOW_DESTROY) {
            super.dispose();
        } // end if
        return true;
    } // end handleEvent
0
 
JavatmCommented:
> I'm getting errors:

> cannot resolve symbol
> symbol  : class JFrame
> class PF extends JFrame {

you need to import :
import javax.swing.*;

hope that helps . . .
Javatm
0
 
luna621Author Commented:
Swing!  Okay, thanks!
0
 
luna621Author Commented:
Thank you objects and Javatm!  Now I know what deprecated means and some ways of dealing with it.  I will continue to experiment and change my program.  Keep up the good work!

-luna621 =^^=  
0
 
objectsCommented:
> YourFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);

you dont want to do that

>            System.exit(0);

or that
0
 
JavatmCommented:
http://www.freewebs.com/cube-j

Always Glad 2 Help . . .
Friend : Javatm
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

  • 10
  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now