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:
|standard container class
|Manages several other container classes via tabs.
|In a two-part container (horizontal or vertical), the size of the split can be changed.
|Allows scrolling within a container. Often used with tables.
|Toolbar that can be equipped with any icons for quick access to certain functions.
|A JDesktopPane can contain internal windows (JInternalFrame).
|Typically added to a JDesktopPane .
|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:
Java. long . Object
extended by java. awt . Component
extended by java. awt . Container
extended by javax. swing . JComponent
extended by CONTAINER CLASSES
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:
|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.
|The width and height of the container can be set using the setSize method.
|With setVisible you can control the visibility of any control element or container class.
|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.
|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.
|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.