Sign in to follow this  
andrewk3652

[java] Dialogs in Applets

Recommended Posts

andrewk3652    252
Dialogs require a parent Frame in their constructor, but Applets inherit from Panel (and then Container), not Frame. Specifically, in JDK 1.5, Panel's inheritance tree is:
java.lang.Object
  java.awt.Component
      java.awt.Container
          java.awt.Panel
              java.applet.Applet
Now I had heard somewhere that you could try calling the Applet's getParent() method recursively until you reached the parent Frame, but this should be impossible. But it isn't. Namely, the following bit of code works fine:
        Container parent = this.getParent();
        while ( !(parent instanceof Frame) && parent != null ) {
            parent = parent.getParent();
        }
        Frame pFrame = (Frame)parent;
        
        BJConfirmDialog d = new BJConfirmDialog(pFrame, "Test", "Click OK", 100, 100, 100, 100);


BJConfirmDialog is a custom class I have for displaying a standard confirmation dialog with a message, an OK and cancel button. This code works. Now riddle me this - if Applet is extended from the tree listed above, then where is the Frame in there coming from?? I suppose I should just be thankful it works. I'm just curious as to WHY it works. *scratches head*

Share this post


Link to post
Share on other sites
Horizon    145
getParent() has nothing to do with inheritance, it simply returns the Container that contains the Component on which it is called.

Example: If you add a "Panel name" to your "Frame" and then call name.getParent() you will get a reference to your Frame.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this