Container events are fired by a Container
just after a component is added to or removed from the container. These events are for notification only — no container listener need be present for components to be successfully added or removed.
The following example demonstrates container events. By clicking Add a button or Remove a button, you can add buttons to or remove them from a panel at the bottom of the window. Each time a button is added to or removed from the panel, the panel fires a container event, and the panel's container listener is notified. The listener displays descriptive messages in the text area at the top of the window.
Try this:You can find the demo's code in ContainerEventDemo.java
. Here is the demo's container event handling code:
public class ContainerEventDemo ... implements ContainerListener ... { ...//where initialization occurs: buttonPanel = new JPanel(new GridLayout(1,1)); buttonPanel.addContainerListener(this); ... public void componentAdded(ContainerEvent e) { displayMessage(" added to ", e); } public void componentRemoved(ContainerEvent e) { displayMessage(" removed from ", e); } void displayMessage(String action, ContainerEvent e) { display.append(((JButton)e.getChild()).getText() + " was" + action + e.getContainer().getClass().getName() + newline); } ... }The Container Listener API
The ContainerListener Interface
The corresponding adapter class is ContainerAdapter
.
getSource
method. Examples that Use Container Listeners
The following table lists the examples that use container listeners.
Example Where Described NotesContainerEventDemo
This section Reports all container events that occur on a single panel to demonstrate the circumstances under which container events are fired.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4