Jump to content
  • Advertisement
Sign in to follow this  
andrewk3652

[java] Dialogs in Applets

This topic is 4930 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!