JPanel: addContainerListener(ContainerListener l) : JPanel « javax.swing « Java by API






JPanel: addContainerListener(ContainerListener l)

  

import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

  public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel buttonPanel = new JPanel();
    buttonPanel.addContainerListener(new ContainerListener() {
      public void componentAdded(ContainerEvent e) {
        displayMessage(" added to ", e);
      }
      public void componentRemoved(ContainerEvent e) {
        displayMessage(" removed from ", e);
      }
      void displayMessage(String action, ContainerEvent e) {
        System.out.println(((JButton) e.getChild()).getText() + " was" + action
            + e.getContainer().getClass().getName());
      }
    });
    buttonPanel.add(new JButton("A"));

    frame.add(buttonPanel);

    frame.setSize(300, 200);
    frame.setVisible(true);
  }

}
   

   
    
  








Related examples in the same category

1.JPanel: addAncestorListener(AncestorListener lis)
2.JPanel: addMouseListener(MouseListener mous)
3.JPanel: addMouseMotionListener(MouseMotionListener lis)
4.JPanel: addKeyListener(KeyListener l)
5.JPanel: getFocusTraversalKeys(int id)
6.JPanel: isFocusCycleRoot()
7.JPanel: paintComponent(Graphics g)
8.JPanel: repaint()
9.JPanel: setBackground(Color c)
10.JPanel: setBorder(Border border)
11.JPanel: setComponentPopupMenu(JPopupMenu popup)
12.JPanel: setFocusCycleRoot(boolean value)
13.JPanel: setFocusTraversalPolicyProvider(boolean value)
14.JPanel: setLayout(null)
15.JPanel: setOpaque(boolean isOpaque)
16.JPanel: setPreferredSize(Dimension preferredSize)
17.JPanel: setToolTipText(String htmlString)
18.extends JPanel