Click to See Complete Forum and Search --> : Don't understand this line ?


May 8th, 2000, 09:59 AM
Would you like please explain to me what does this line mean :

for(theFrame = getParent(); !(theFrame instanceof Frame) && theFrame != null; theFrame = ((Component)theFrame).getParent());



Thanks

kannanbalu
May 8th, 2000, 10:13 AM
Hi,

for(theFrame = getParent(); !(theFrame instanceof Frame) && theFrame != null; theFrame = ((Component)theFrame).getParent());

The loop starts from the parent of your current
frame and continues till it reaches the top
of your hierarchy(tree).

I am not sure about the condition:
!(theFrame instanceof Frame).
To me it probably would have made sense if
the not ! operator wasn't there. In that case,
it would be from your frame's parent to the
Frame Class.


Kannan

poochi
May 8th, 2000, 11:14 AM
The code which you post trying to get the parent "Frame" from the current component.


for loop structure ..


for( initializer ; condition ; increment ){}



Ex : for( int i = 0 ; i < 10 ; i++ ){}



In your code



Initializer : theFrame = getParent() ( like int i = 0 )


gettting the parent component of the current component.



condition : !( theFrame instanceof Frame ) ( like i < 10 )



checks whether the current component is Frame. If it is , break the loop.



Increment : theFrame = ((Component)theFrame).getParent() ( like i++ )



get the parent of the "theFrame" Component.







More readable code :




public Frame getParentFrame( Component c ){
// c should not be null and should not be Frame
// If it's Frame , break the loop and return it.
while( ( c != null ) && !( c instanceof Frame ){
c = c.getParent();
}
return ( Frame ) c;
}




Poochi..

mbutu
May 10th, 2000, 01:43 AM
It simply tries to find the Frame that the component theFrame has been added to.

getParent() returns the Container of theFrame.

Frame (Container)
^
Panel (Container)
^
Component (theFrame)