Container Classes in java

Container class in java:

Containers Classes are used to structure and group the actual controls. The following table provides a brief overview of the container classes:

Class short description
JPanel standard container class
JTabbedPane Manages several other container classes via tabs.
JSplitPane In a two-part container (horizontal or vertical), the size of the split can be changed.
JScrollPane Allows scrolling within a container. Often used with tables.
JToolBar Toolbar that can be equipped with any icons for quick access to certain functions.
JDesktopPane A JDesktopPane can contain internal windows (JInternalFrame).
JInternalFrame Typically added to a JDesktopPane .
JLayeredPane Differs from the JPanel in that the level of the contained components can be specified, so that they can be pushed to the foreground or background, for example.

Because all container classes derive from the following superclasses, they also inherit their methods:

All container classes, therefore, have many of the same methods. We would like to list some frequently used methods here so that we don’t have to explain them again in my upcoming articles but can focus on the differences there.

Container Methods in Java:

method name short description
add There are different variants of the add method. The simple add method is used to add a component to the container. However, there are still various add methods (such as addKeyListener ) that can be used to assign a specific EventListener to the container. In this way, certain user actions (e.g. keystrokes) can be recognized and treated accordingly.
setSize The width and height of the container can be set using the setSize method.
setVisible With setVisible you can control the visibility of any control element or container class.
getComponent The method getComponent is available in different variants. These serve to receive components located in the container and to be able to address them in this way. For example, the container located at the transferred position can be determined via getComponentAt. The getComponents method returns an array with all components in the respective container.
setLayout A so-called layout manager can be set using the setLayout method. This controls the display of the individual elements. Layouts are explained in more detail in a separate article.
remove The remove method also comes in different flavors. This does exactly the opposite of add, ie elements or EventListeners added to the container can be removed with remove.

Listing all methods would go beyond the scope. See the Java API for this.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button