setVisible( boolean ) controls whether a Component and its children are displayed on the screen. setVisible( false ) hides a Component by
marking it as invisible. setVisible( true ) reveals a Component by
marking it as visible. hide() and show() are now deprecated.
How SetVisible Works
With setVisible( false ), if the Component is not
already marked invisible, setVisible calls invalidate() which invalidates the Container’s layout and the chain of parents since there is now
more screen real estate in the Container and the positions
of the siblings must be adjusted to flow into the freed space.
With setVisible( true ),
if the Component is not already marked visible,
setVisible calls invalidate()
which invalidates the Container’s layout and the
chain of parents since there is now less screen real estate in the Container and the positions of the siblings must be adjusted to
squeeze in this new Component.
Further, setVisible will schedule a repaint if the
visibility status actually changed. However, it won’t schedule the Container.validate() automatically. That is up to you to do after all
your visibility changes to the Components are done.
show() unhides a component by marking it as visible,
allowing it to be painted again on the next painting cycle. show is deprecated. With Java version 1.1 or later
you are supposed to use setVisible( true);
A Component is considered visible if in on any repainting cycle, it should be repainted. It might
not actually be visible on the screen right now because it is occluded, or because
CardLayout is not currently displaying the panel in which
the component lives.
How are invisible elements treated?
It depends on the LayoutManager you choose.
LayoutManagers are free to choose either strategy. You
will have to experiment to find out. If your LayoutManager uses the first strategy, but you never get around to
calling Container.validate(), your program will behave as
if it had used the second.
- Do they act as if they weren’t there at all? Do the other elements move
into take over the vacated space? Is the visual effect the same as removing them
from the enclosing Container?
- Is there just a blank hole marking their place?
setVisible has no effect on the visibily flags of the
children. Unlike some GUI (Graphic User Interface)
programming systems, when you parent.setVisible( true )
you don’t disturb any of the children’s visibility flags and the hidden
children will stay hidden, even when you again reveal the parent.
Composing A Frame
While you are composing a Frame,
you usually would temporarily setVisible( false ), waiting until all the Components are added, before allowing any validate/ repaints to be triggered, then
masterFrame.validate() to compute the layouts then,
masterFrame.setVisible( true ) when you were ready for
your masterwork to be revealed.
Unlike ordinary Components, Windows, Frames and JFrames conveniently start out life automatically with setVisible( false ). Traditionally you do the validate() and setVisible( true ) in the code that calls the Frame’s constructor not in the Frame’s constructor or addNotify
Temporarily Hiding Components
For efficiency and to avoid a jittery screen,
you should first do all your setVisible( true ) and
setVisible( false ) calls
which will invalidate the layout if anything actually changed. Then when you have
Component visibility set the way you want, do your
Container.validate() to redo the layout if necessary and
repaint if necessary.
hide() make temporarily invisible. hide is deprecated. With Java version 1.1 or later
you are supposed to use setVisible( false); instead. hide is also used to
describe what one window does to another when it is painted over top of it.
In summary, the only time you routinely need setVisible is after a Frame or
Component has been constructed. At other times you can use
it to temporarily hide or reveal Components. Remember to
call Container.validate() to adjust the layout for the
newly created or hidden Components. Java can’t
schedule that automatically for you, since it has no way of knowing when your batch
of changes is complete. A sign you have forgotten the validate is that you need to drag your Frame to a new size for your application to work. Resizing will
schedule an invalidate.
Oracle’s Javadoc on setVisible
class : available: