Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Java
Expertise: Intermediate
May 3, 1999



Application Security Testing: An Integral Part of DevOps

Specifying Position When Adding Components

The java.awt.Container class provides a number of different overloaded versions of the add() method which are used to add components to the container. The integer value specified with the add(Component, int) method represents the component's position within the container's list of subcomponents. This position should not be confused with the display location, which is controlled by the layout manager used (if any), but instead represents the position within a list. When the add(Component) method is called, the component is added to the end of the list, but add(Component, int) allows you to specify the position where the component should be inserted.

The position of objects is important in several ways. First, it will determine the order in which components are added to the display. For example:

setLayout(new GridLayout(1, 2, 10, 0));
add(new Label("Added this one first"));
add(new Label("Added this one second, 0));
The second label added will actually be displayed "before" (i.e., to the left of) the first one added, because its position of 0 causes it to be inserted into the array prior to any that were previously added. Another important characteristic of components that is affected by their position is that of the "z-order." Z-order refers to which components are "in front" of each other on the screen, and is especially important when two components overlap each other. If one component has a lower z-order than another, then it is considered to be in front of that component, and will be drawn "on top" of it. In the previous example, if the two labels overlapped each other on the screen, the second label would be drawn "above" the first. Although it is not possible to change the position/z-order of a component once it has been added, you can call the remove() method on the container and then add() the component again, specifying the desired position/order.
Brett Spell
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date