Solved

Applet - window popup message

Posted on 2004-04-30
23
1,503 Views
Last Modified: 2013-11-23
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
Comment
Question by:luna621
  • 10
  • 7
  • 6
23 Comments
 

Author Comment

by:luna621
ID: 10965614
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
 
LVL 92

Expert Comment

by:objects
ID: 10965618
> 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
 

Author Comment

by:luna621
ID: 10965623
Ooo... that's bad.  Is there a way to fix it?
0
 
LVL 92

Expert Comment

by:objects
ID: 10965636
depends on what versions of Java you want to support.
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10965642
> 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
 
LVL 15

Expert Comment

by:Javatm
ID: 10965648
> 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
 

Author Comment

by:luna621
ID: 10965665
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
 
LVL 15

Expert Comment

by:Javatm
ID: 10965679

> 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
 

Author Comment

by:luna621
ID: 10965680
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
 
LVL 92

Expert Comment

by:objects
ID: 10965683
> 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
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 10965687
or you could replace it with the processEvent() method.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

by:Javatm
ID: 10965696
Try using a different approach !.
0
 

Author Comment

by:luna621
ID: 10965703
Comment from objects
Date: 04/30/2004 05:44PM HST

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

The processEvent() method??
 
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10965709
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
 
LVL 92

Expert Comment

by:objects
ID: 10965757
> The processEvent() method??

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

Author Comment

by:luna621
ID: 10965775
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
 

Author Comment

by:luna621
ID: 10965776
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
 

Author Comment

by:luna621
ID: 10965793
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
 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 100 total points
ID: 10965808
> 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
 

Author Comment

by:luna621
ID: 10965810
Swing!  Okay, thanks!
0
 

Author Comment

by:luna621
ID: 10965815
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
 
LVL 92

Expert Comment

by:objects
ID: 10965823
> YourFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);

you dont want to do that

>            System.exit(0);

or that
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10965854
http://www.freewebs.com/cube-j

Always Glad 2 Help . . .
Friend : Javatm
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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:

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now