Java Tutorial/Swing Event/ContainerListener

Материал из Java эксперт
Перейти к: навигация, поиск

ContainerListener and ContainerEvent

/*
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * ContainerEventDemo.java requires no other files.
 */
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
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 java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ContainerEventDemo extends JPanel implements ContainerListener,
    ActionListener {
  JTextArea display;
  JPanel buttonPanel;
  JButton addButton, removeButton, clearButton;
  Vector<JButton> buttonList;
  static final String ADD = "add";
  static final String REMOVE = "remove";
  static final String CLEAR = "clear";
  static final String newline = "\n";
  public ContainerEventDemo() {
    super(new GridBagLayout());
    GridBagLayout gridbag = (GridBagLayout) getLayout();
    GridBagConstraints c = new GridBagConstraints();
    // Initialize an empty list of buttons.
    buttonList = new Vector<JButton>(10, 10);
    // Create all the components.
    addButton = new JButton("Add a button");
    addButton.setActionCommand(ADD);
    addButton.addActionListener(this);
    removeButton = new JButton("Remove a button");
    removeButton.setActionCommand(REMOVE);
    removeButton.addActionListener(this);
    buttonPanel = new JPanel(new GridLayout(1, 1));
    buttonPanel.setPreferredSize(new Dimension(200, 75));
    buttonPanel.addContainerListener(this);
    display = new JTextArea();
    display.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(display);
    scrollPane.setPreferredSize(new Dimension(200, 75));
    clearButton = new JButton("Clear text area");
    clearButton.setActionCommand(CLEAR);
    clearButton.addActionListener(this);
    c.fill = GridBagConstraints.BOTH; // Fill entire cell.
    c.weighty = 1.0; // Button area and message area have equal height.
    c.gridwidth = GridBagConstraints.REMAINDER; // end of row
    gridbag.setConstraints(scrollPane, c);
    add(scrollPane);
    c.weighty = 0.0;
    gridbag.setConstraints(clearButton, c);
    add(clearButton);
    c.weightx = 1.0; // Add/remove buttons have equal width.
    c.gridwidth = 1; // NOT end of row
    gridbag.setConstraints(addButton, c);
    add(addButton);
    c.gridwidth = GridBagConstraints.REMAINDER; // end of row
    gridbag.setConstraints(removeButton, c);
    add(removeButton);
    c.weighty = 1.0; // Button area and message area have equal height.
    gridbag.setConstraints(buttonPanel, c);
    add(buttonPanel);
    setPreferredSize(new Dimension(400, 400));
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
  }
  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);
    display.setCaretPosition(display.getDocument().getLength());
  }
  /*
   * This could have been implemented as two or three classes or objects, for
   * clarity.
   */
  public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
    if (ADD.equals(command)) {
      JButton newButton = new JButton("JButton #" + (buttonList.size() + 1));
      buttonList.addElement(newButton);
      buttonPanel.add(newButton);
      buttonPanel.revalidate(); // Make the button show up.
    } else if (REMOVE.equals(command)) {
      int lastIndex = buttonList.size() - 1;
      try {
        JButton nixedButton = buttonList.elementAt(lastIndex);
        buttonPanel.remove(nixedButton);
        buttonList.removeElementAt(lastIndex);
        buttonPanel.revalidate(); // Make the button disappear.
        buttonPanel.repaint(); // Make the button disappear.
      } catch (ArrayIndexOutOfBoundsException exc) {
      }
    } else if (CLEAR.equals(command)) {
      display.setText("");
    }
  }
  /**
   * Create the GUI and show it. For thread safety, this method should be
   * invoked from the event-dispatching thread.
   */
  private static void createAndShowGUI() {
    // Create and set up the window.
    JFrame frame = new JFrame("ContainerEventDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Create and set up the content pane.
    JComponent newContentPane = new ContainerEventDemo();
    newContentPane.setOpaque(true); // content panes must be opaque
    frame.setContentPane(newContentPane);
    // Display the window.
    frame.pack();
    frame.setVisible(true);
  }
  public static void main(String[] args) {
    // Schedule a job for the event-dispatching thread:
    // creating and showing this application"s GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }
}





Determining When a Component Is Added or Removed from a Container

import java.awt.ruponent;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import javax.swing.JFrame;
public class Main {
  public static void main() {
    ContainerListener listener = new ContainerAdapter() {
      public void componentAdded(ContainerEvent evt) {
        
        Component c = evt.getChild();
      }
      public void componentRemoved(ContainerEvent evt) {
        
        Component c = evt.getChild();
      }
    };
    JFrame frame = new JFrame();
    frame.addContainerListener(listener);
  }
}





How to Write a Container Listener

Container events are fired after a component is added to or removed from the container.



import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class UsingContainerListener {
  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);
  }
}





Using ComponentListener to catch the JFrame Maximization event

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ruponentEvent;
import java.awt.event.ruponentListener;
import javax.swing.JFrame;
public class Main extends JFrame implements ComponentListener {
  public Main() {
    addComponentListener(this);
  }
  public void componentHidden(ComponentEvent e) {
    System.out.println("componentHidden");
  }
  public void componentMoved(ComponentEvent e) {
    System.out.println("componentMoved");
  }
  public void componentResized(ComponentEvent e) {
    System.out.println("componentResized");
    if (getState() == Frame.ICONIFIED) {
      System.out.println("RESIZED TO ICONIFIED");
    } else if (getState() == Frame.NORMAL) {
      System.out.println("RESIZED TO NORMAL");
    } else {
      System.out.println("RESIZED TO MAXIMIZED");
    }
  }
  public void componentShown(ComponentEvent e) {
  }
  public static void main(String[] arg) {
    Main m = new Main();
    m.setVisible(true);
    m.setSize(new Dimension(300, 100));
    m.setLocation(50, 50);
  }
}