Java Glossary : setVisible

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

You are here : home : Java Glossary : S words : setVisible.

setVisible
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.

How are invisible elements treated?

  1. Do they act as if they weren't there at all? Do the other elements move in to take over the vacated space? Is the visual effect the same as removing them from the enclosing Container?
  2. Is there just a blank hole marking their place?
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.

setVisible has no effect on the visibily flags of the children. Unlike some GUI 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 method.

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.

Summary

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/

Difference Between Window Types

What are the differences between a Applet, Canvas, Component, Container, Dialog, Frame, JApplet, JComponent, JContainer, JDialog, JInternalFrame, JPanel, JFrame, JWindow, Panel and Window?

When the following table suggests a Component is for AWT but not Swing, it means there is an improved Swing component. In theory you could use the old AWT component, but normally you would not want to.

When it asks if Components are visible, it means, are the visible by default when first created. Obviously you set do a setVisible( true ) later. The rule of thumb is, freestanding windows start out invisible and everything else starts out visible. Another way of looking at it is, everything derived from Window starts out invisible.

The paned refers to whether you use getContentPane.setLayout or simply setLayout.

Differences Between Various Types of Windows
class What Is It AWT Swing Visible Paned Derived From Default Layout
Applet represents the featureless Window provided by the browser for an Applet to run in. Y N true N Panel FlowLayout
Button Not a window. It is here is an example of a Component. Y N true N Component n/a
Canvas an area you can use the low level drawing tools on, rather than dropping in components placed with a LayoutManager. Raw material for creating your own components. Just a place you can draw. It cannot contain Components. For Swing, use a JPanel instead. Y N true N Component n/a
Component Not a window. It is an abstract class underlying Buttons etc. Y N true N Object n/a
Container These are the basis on which all the other windows are built. They manage the child Components and LayoutManager. They are missing an addNotify method to create the peer object, so can't appear on screen. You don't normally instantiate Containers directly, but some subclass of them. Y Y true N Component null
Dialog A pop-up box to deliver an error message or alert. Temporary Window for displaying information or requesting keystrokes. It requires a parent Frame, thus in cannot be used inside an Applet which has no Frame. It can be modal, which means it blocks input to all other Windows until it is dismissed. It must have a Frame mentioned in the constructor. Y N false N Window BorderLayout
Frame A resizable, movable window with title bar and close button. Usually it contains Panels. Y N false N Window BorderLayout
JApplet represents the featureless Window provided by the browser for an Applet to run in. N Y true Y Applet FlowLayout
JButton Not a window. It is here is an example of a JComponent. N Y true N AbstractButton, JComponent n/a
JComponent Not a window. It is an abstract class underlying JButtons etc. N Y true N Container, not Component, Container! n/a
JContainer There is no such beast! Since Containers have no on screen aspect, the ordinary AWT Container suffices. n/a n/a n/a n/a n/a n/a
JDialog A pop-up box to deliver an error message or alert. Usually created with JOptionPane methods. Temporary Window for displaying information or requesting keystrokes. It requires a parent JFrame, thus in cannot be used inside an JApplet which has no JFrame. It can be modal, which means it blocks input to all other JWindows until it is dismissed. You can place complex arrays of Components on JDialogs, not just simple error messages. N Y false Y Dialog BorderLayout
JFrame A resizable, movable window with title bar and close button. Usually it contains JPanels. The entire application is usually a JFrame. N Y false Y Frame BorderLayout
JInternalFrame Independent windows that the user can resized and move, but only within an enclosing JFrame. N Y false Y JComponent BorderLayout
JOptionPane A modal pop-up box to deliver an error message or alert. Temporary Window for displaying information or requesting keystrokes. It requires a parent JFrame, thus in cannot be used inside an JApplet which has no JFrame. Unlike a JDialog, JOptionPanes are always modal, which means they block input to all other JWindows until they are dismissed. N Y false Y JDialog BorderLayout
JPanel A region internal to a JFrame or another JPanel. Used for grouping components together. Optionally bounded by a visible border. Lives inside some enclosing Container. N Y true N JComponent FlowLayout
JWindow A window without a title bar or move controls. The program can move and resize it, but the user cannot. It has no border at all. N Y false Y Window BorderLayout
Panel A region internal to a Frame or another Panel. Used for grouping components together. Not bounded by a visible border. You can change background colour of a panel to delimit it though. Lives inside some enclosing Container. Y N true N Container FlowLayout
Window A window without a title bar or move controls. The program can move and resize it, but the user cannot. Free standing Window, not inside any other Window. It must have Frame mentioned in the constructor. Y N false N Container BorderLayout

validate.


CMP logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 2807.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/setvisible.html J:\mindprod\jgloss\setvisible.html