package examples; import java.awt.*; import java.awt.event.*; import javax.swing.*; /* * @author Kenneth J. Goldman, Washington University, kjg@wustl.edu * Created: Feb 3, 2005 * Modified: March 30, 2006 * */ public class PanelWithOverlay extends JLayeredPane { private static final long serialVersionUID = 1L; JComponent content; /** * Creates an PanelWithOverlay with the given content. * The content is resized whenever the PanelWithOverlay is resized. * @param content the component to be displayed in the default layer */ public PanelWithOverlay(final JComponent content) { this.content = content; content.setSize(content.getPreferredSize()); add(content); addComponentListener(new ComponentAdapter() { public void componentResized(ComponentEvent arg0) { content.setSize(getSize()); content.revalidate(); } }); } /** * Raises the given component to the drag layer. * MODIFIES: x, x.parent(), this * EFFECTS: If x is not already a child of this JLayeredPane, x is added to * the drag layer. The component's position is converted to the coordinate system of * the layered panel, so that it is appears to stay in the same place. * @param x the Component to be moved to the overlay plane */ void raiseToOverlay(Component x) { x.setLocation(SwingUtilities.convertPoint(x.getParent(), x.getLocation(), this)); add(x,JLayeredPane.DRAG_LAYER); } /** * Drops the given component from the drag layer into the content component, placing it in * the content's container that is at the given point. * MODIFIES: x, this, and the container beneath the cursor in the content panel * EFFECTS: If the given cursor position is above a JComponent in the content panel, then * x is added to that container * @param x * @param cursor the position of the drop * @return true if the cursor was above a container, and x was therefore dropped in */ boolean dropToContainerBeneath(Component x, Point cursor) { //Get the top level component under the cursor: Component beneath = content.getComponentAt(cursor); //Alternatively, allow component to be placed inside of the deepest component: //Component beneath = SwingUtilities.getDeepestComponentAt(content,cursor.x,cursor.y); if (beneath instanceof JComponent) { JComponent destination = (JComponent) beneath; destination.add(x); destination.revalidate(); // force the layout manager to run repaint(); return true; } return false; } /** * Drops the given component (from the drag layer) into the content component, placing it in * the content's container that is at the upper-left corner of the given component. * MODIFIES: x, this, and the container beneath component in the content panel * EFFECTS: If the given component is above a JComponent in the content panel, then * x is added to that container . * @param x * @return true if the component was was above a JComponent in the content panel */ boolean dropToContainerBeneath(Component x) { return dropToContainerBeneath(x, x.getLocation()); } public Dimension getPreferredSize() { return content.getPreferredSize(); } }