Results 1 to 1 of 1
-
Demonstrating the ContainerListener
Java Code: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 ContainerTest { 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(); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
Demonstrating the ComponentListener
By Java Tip in forum java.awtReplies: 0Last Post: 04-23-2008, 08:22 PM -
Demonstrating the AncestorListener
By Java Tip in forum java.awtReplies: 0Last Post: 04-23-2008, 08:21 PM -
Demonstrating the AdjustmentListener
By Java Tip in forum java.awtReplies: 0Last Post: 04-23-2008, 08:21 PM -
Demonstrating the ActionListener
By Java Tip in forum java.awtReplies: 0Last Post: 04-23-2008, 08:20 PM -
Demonstrating the WeakHashMap
By Java Tip in forum java.langReplies: 0Last Post: 04-12-2008, 08:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks