wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Differnece between setVisible() and show() methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Differnece between setVisible() and show() methods" Watch "Differnece between setVisible() and show() methods" New topic
Author

Differnece between setVisible() and show() methods

Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107
Hi All,
Is there is difference between a call to setVisible (true) of the Component class and a call to show() method of the Window class?
Khalid A. Mughal's book on programmer certification study mentions that the setVisible(true) makes the window visible, but does not bring it to the front; whereas the show() method of the Window class makes a window visible and also brings it to the front of other windows.
I tried the following example, but could not make out any difference in the behaviour between the two methods.

I commented the calls to setVisible() and show() methods alternatively but could find no difference in behaviour between the two. Both the methods bring the window to the front. Can anybody help me with this? Am I missing something?
Regards.
Kezia.

---I cleaned up the code to prevent running off the side -- Carl
[This message has been edited by Carl Trusiak (edited November 20, 2001).]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
In my testings I haven't been able to find a difference between show and setVisible.
When reading the API it seems to me that show should brings a Window to the front if it is already visible. But, is this meaning that another Window is partially over it? Not possible because Windows can not be added to a Container (IllegalArgumentexception). Well, a Dialog can be over another Window, but in my program calling Window.show() didn't place it over the Dialog.
I have searched the AWT Sun forum and these are related posts:
http://forum.java.sun.com/thread.jsp?forum=5&thread=8224 http://forum.java.sun.com/thread.jsp?forum=5&thread=156007 http://forum.java.sun.com/thread.jsp?forum=5&thread=9616


SCJP2. Please Indent your code using UBB Code
Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107

Thank you Jose for your reply.
Here's an example that I tried out. I created a dialog as a child of a frame and gave calls to setVisible (true) and show () methods alternatively. Seems like both the methods make both the dialog and the frame visible and also bring them to the front.

Regards.
Kezia.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Just set the location for the Dialog to 0,0 and it will be under the the Frame. Now once the Dialog is shown, the frame never gets placed over the Dialog again. So this method is no valid.
The third post I gave tell us how to change the Z-order for a Component contained in a Container. Note that there is a flickering because a Component is added and removed.
My guessing is that bringing something to the front is Z-order related. Because a dialog is not contained by its owner I am not expecting that changing the Z-order will work for Dialogs. Because Windows can not be added to a Container I am not expecting Z-order capabilities for Windows either.
It follows a test that shows how Button1 is placed over Button2 when pushing Button3.

hope helps
Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107


Just set the location for the Dialog to 0,0 and it will be under the the Frame. Now once the Dialog is shown, the frame never gets placed over the Dialog again. So this method is no valid.

I don't agree with you. In the following program, I have set the location of the Dialog to 0,0 and when I call either setVisible(true) or show() on the Frame, it does bring the frame to the front.

Regards,
Kezia.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
No, it doen't bring the frame to the top because the Dialog is still clipping it. See the difference with the program that brings a button over other button.
Guennadiy VANIN
Ranch Hand

Joined: Aug 30, 2001
Posts: 898
show() is deprecated in favor of setVisible()
show(boolean b) in java.awt.component has the only line in it, guess what
and nothing else.
Just to make consistent with naming policy, Sun also started to have it though later than javaranch!
[This message has been edited by G Vanin (edited November 20, 2001).]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by G Vanin:
setVisible(boolean b) in java.awt.component has the only line in it, guess what and nothing else.

And show(b) calls show().

Also show() in Component is deprecated.
show in Window is not deprecated but overrides show in Component.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Guennadiy VANIN
Ranch Hand

Joined: Aug 30, 2001
Posts: 898
I am sorry, I was not following...
So, why to deprecate a show() in superclass (Component) and not doing it in subclass (Window)?

From David M.Geary Graphic Java 1.2. Vol.1 AWT. The Sun Microsystems Press, Chapter 16, p.574
AWT tip...
Invoke toFront() After show() for Windows That Are not Visible
Window.show() is guaranteed to bring the window being shown to the front only if the window is currently visible.
If the window is not visible, show() makes no such guarantee.
Therefore, to ensure that a window being shown will wind up in front of all other windows, invoke toFront() after calling show().

[This message has been edited by G Vanin (edited November 20, 2001).]
Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107
Sorry Jose. You are right. show() or setVisible(true) only brings the repective window to focus and not to the front.
In the following code, I have given a call to toFront() method after giving calls to setVisible(true) followed by show().

I am still unsuccessful in bringing the parent frame to the front. Wonder, whether I am missing out something. Could anybody give me a solution?
Thanks.
Kezia.

[This message has been edited by Kezia Matthews (edited November 21, 2001).]
Guennadiy VANIN
Ranch Hand

Joined: Aug 30, 2001
Posts: 898
Kezia,
I beleive you are mixing, just mixing them , how I understand you. Here is the part from that link:



<h2>Z-order limitations</h2>

It is Issue No. 4 -- the one that
applies to overlapping components --� that causes the most
grief for developers who are mixing AWT and Swing� components.
This limitation exists because of the fact that a lightweight�
component re-uses the screen real estate of its nearest heavyweight
ancestor� and is therefore restricted to the z-order position
of that ancestor.� To illustrate, say your application had
the following containment hierarchy� (where "H" means
heavyweight and "L" means lightweight):










The problem that shifting from "Frame." to "JFrame.getContentPane()." does not change anything. I do not know abt Sun's explanations but developers in my company consider that JDialog is always on top, and it is used here heavily just to tap any possible other applications, and oblige client do not deviate himself from
[This message has been edited by G Vanin (edited November 21, 2001).]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello G
But in our examples we are not mixing LightWeights and HeavyWeights components.
Guessing : Z order is only related to Components hold by the same Container. Why I think so? because I have only seen the z order specified when adding components to a container. We are discussing AWT only.
Also reading the three urls I gave it seems to me there is no easy way to keep a Window always on top.
Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107

I think that the only way to make the parent frame come to the front after the child dialog is displayed on top of the frame, is to give a call to dispose off the child window i.e, the dialog through the dispose() method. To make the dialog, again visible and come to the front, the pack() function may be called on the dialog, followed by the show() function.
Thanks to all of you for the responses.
Regards,
Kezia.
 
GeeCON Prague 2014
 
subject: Differnece between setVisible() and show() methods