Demonstrating the ComponentListener : Various Event Listener « Swing JFC « Java






Demonstrating the ComponentListener

Demonstrating the ComponentListener
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

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

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

    ComponentListener comp = new ComponentListener() {
      public void componentHidden(ComponentEvent e) {
        dump("Hidden", e);
      }

      public void componentMoved(ComponentEvent e) {
        dump("Moved", e);
      }

      public void componentResized(ComponentEvent e) {
        dump("Resized", e);
      }

      public void componentShown(ComponentEvent e) {
        dump("Shown", e);
      }

      private void dump(String type, ComponentEvent e) {
        System.out.println(e.getComponent().getName() + " : " + type);
      }
    };

    JButton left = new JButton("Left");
    left.setName("Left");
    left.addComponentListener(comp);

    final JButton right = new JButton("Right");
    right.setName("Right");
    right.addComponentListener(comp);

    ActionListener action = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        right.setVisible(!right.isVisible());
      }
    };
    left.addActionListener(action);

    JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
        left, right);

    contentPane.add(pane, BorderLayout.CENTER);

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








Related examples in the same category

1.Demonstrating the WindowListener with a WindowAdapterDemonstrating the WindowListener with a WindowAdapter
2.Demonstrating the ActionListenerDemonstrating the ActionListener
3.Demonstrating the AdjustmentListenerDemonstrating the AdjustmentListener
4.Demonstrating the AncestorListener
5.Demonstrating the ContainerListenerDemonstrating the ContainerListener
6.Demonstrating the FocusListenerDemonstrating the FocusListener
7.Demonstrating the HyperlinkListenerDemonstrating the HyperlinkListener
8.Demonstrating the InternalFrameListenerDemonstrating the InternalFrameListener
9.Demonstrating the ItemListenerDemonstrating the ItemListener
10.Demonstrating the KeyListenerDemonstrating the KeyListener
11.Demonstrating the MenuListenerDemonstrating the MenuListener
12.Demonstrating the MouseListener and MouseMotionListenerDemonstrating the MouseListener and MouseMotionListener
13.Demonstrating the MouseWheelListenerDemonstrating the MouseWheelListener
14.Demonstrating the PopupMenuListenerDemonstrating the PopupMenuListener
15.Demonstrating the WindowListener
16.Responding to KeystrokesResponding to Keystrokes