[java] Dialogs in Applets

Started by
2 comments, last by CaptainJester 19 years, 5 months ago
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*
=========================Buildium. Codium. Fragium.http://www.aklabs.net/=========================
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.
Oh, okay.

I thought getParent() was getting the parent class. :-)

My bad.
=========================Buildium. Codium. Fragium.http://www.aklabs.net/=========================
You can pass null to that constructor. It just means that it can't be modal.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement