Absolute Positioning Without a Layout Manager
Although there's rarely a reason to do so, you can completely avoid using a layout manager when designing
an interface. However, if you don't use a layout manager, you are responsible for explicitly setting the size
and position of each component within a container using Component methods such as setSize(),
setLocation(), and setBounds(). This approach is rarely desirable, because it usually results in an
interface that must be revised to appropriately handle even minor changes.
Although using absolute positioning without a layout manager can prevent using a more complicated mixture
of layout managers, since Java is so portable the application could well be run on a device with a display
smaller than that on which it was originally designed. If you know the smallest likely size of the screen the
application will run on, you can use absolute positioning with a non-resizable window
If you wish to remove the layout manager from a container and explicitly set the size and position of the
components in that container, you can call the setLayout() method and specify a null value, as shown below:
JPanel panel = new JPanel();
panel.setLayout(null);
When a container displays its child components, it does so using the position and size values assigned to
those components, which are usually set by a layout manager. If you add a component to a container and do
not set the component's location, it will appear at the container's origin (in other words, at coordinates 0, 0).
However, if you add a component to a container and do not specify the component's size, it will not appear
at all, because its width and height values will both be 0. The preferred, minimum, and maximum size values
are used by layout managers to determine the size that should be used for a component, but components are
not automatically set to any of those three sizes when created.
Invisible Components
Components that have their visibility flag set to false do not appear when their parent container is displayed,
and you can query and modify the visibility flag using Component's isVisible() and setVisible()
methods. In general, layout managers ignore invisible components inside their layoutContainer() method,
causing the container to be formatted as though the invisible components had not been added.
You'll most often use invisible components when some portion of your user interface should not always be
displayed. For example, your interface might have a menu item that allows the user to toggle the display
status of some element such as a tool bar or status bar. In that case, you could add the element to the
container when the container is being constructed, but make it invisible until it should be displayed.
Depending upon the superclass of the component that's made visible or invisible, it may be necessary for you
to use revalidate() to cause the layout manager to reposition and resize the components in the container.
JComponent subclasses automatically trigger this behavior, but others do not.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.