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






JFrame: addContainerListener(ContainerListener l)1

  

import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;

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

public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();

    ContainerListener cont = new ContainerListener() {
      ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          System.out.println("Selected: " + e.getActionCommand());
        }
      };

      public void componentAdded(ContainerEvent e) {
        Component c = e.getChild();
        if (c instanceof JButton) {
          JButton b = (JButton) c;
          b.addActionListener(listener);
        }
      }

      public void componentRemoved(ContainerEvent e) {
        Component c = e.getChild();
        if (c instanceof JButton) {
          JButton b = (JButton) c;
          b.removeActionListener(listener);
        }
      }
    };

    contentPane.addContainerListener(cont);

    contentPane.setLayout(new GridLayout(3, 2));
    contentPane.add(new JButton("First"));
    contentPane.add(new JButton("Second"));
    contentPane.add(new JButton("Third"));
    contentPane.add(new JButton("Fourth"));
    contentPane.add(new JButton("Fifth"));

    frame.setSize(300, 200);
    frame.show();
  }
}

   
    
  








Related examples in the same category

1.JFrame.DISPOSE_ON_CLOSE
2.JFrame.DO_NOTHING_ON_CLOSE
3.JFrame.EXIT_ON_CLOSE
4.JFrame: addComponentListener(ComponentListener l)
5.JFrame: addMouseWheelListener(MouseWheelListener l)
6.JFrame: addWindowListener(WindowListener lis)
7.JFrame: getAccessibleContext()
8.JFrame: getContentPane().getActionMap() [Bind key action to JFrame]
9.JFrame: getDefaultCloseOperation()
10.JFrame: getFontMetrics(Font font)
11.JFrame: getGlassPane()
12.JFrame: getLayeredPane()
13.JFrame: isFocusOwner()
14.JFrame: isUndecorated()
15.JFrame: processWindowEvent(WindowEvent e)
16.JFrame: setAlwaysOnTop(boolean alwaysOnTop)
17.JFrame: setBackground(Color c)
18.JFrame: setCursor(Cursor c)
19.JFrame: setDefaultButton(JButton defaultButton)
20.JFrame: setDefaultButton(JButton bn) (Remove 'Enter' key stroke from JTextField)
21.JFrame: setDefaultCloseOperation(int option)
22.JFrame: setDefaultLookAndFeelDecorated(boolean b)
23.JFrame: setExtendedState(int state)
24.JFrame: setFocusable(boolean focusable)
25.JFrame: setFocusableWindowState(boolean focusableWindowState)
26.JFrame: setFocusTraversalPolicy(FocusTraversalPolicy lo)
27.JFrame: setIconImage(Image image)
28.JFrame: setJMenuBar(JMenuBar bar)
29.JFrame: setLocation(int x, int y)
30.JFrame: setLocationByPlatform(boolean locationByPlatform)
31.JFrame: setLocationRelativeTo(Component c)
32.JFrame: setMaximizedBounds(Rectangle bounds)
33.JFrame: setSize(int width, int height)
34.JFrame: setTitle(String title)
35.JFrame: setUndecorated(boolean b)
36.JFrame: setVisible(boolean visible)