FileDocCategorySizeDatePackage
WindowSnapper.javaAPI DocExample1517Mon Jan 09 11:01:58 GMT 2006None

WindowSnapper

public class WindowSnapper extends ComponentAdapter

Fields Summary
private boolean
locked
private int
sd
Constructors Summary
public WindowSnapper()

 
Methods Summary
public voidcomponentMoved(java.awt.event.ComponentEvent evt)

	
        
        if(locked) return;
        Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
        int nx = evt.getComponent().getX();
        int ny = evt.getComponent().getY();
        // top
        if(ny < 0+sd) {
            ny = 0;
        }
        // left
        if(nx < 0+sd) {
            nx = 0;
        }
        // right
        if(nx > size.getWidth()-evt.getComponent().getWidth()-sd) {
            nx = (int)size.getWidth()-evt.getComponent().getWidth();
        }
        // bottom
        if(ny > size.getHeight()-evt.getComponent().getHeight()-sd) {
            ny = (int)size.getHeight()-evt.getComponent().getHeight();
        }
		// make sure we don't get into a recursive loop when the
		// set location generates more events
        locked = true;
        evt.getComponent().setLocation(nx,ny);
        locked = false;
    
public static voidmain(java.lang.String[] args)

        JFrame frame = new JFrame("Hack #123: Window Snapping");
        JLabel label = new JLabel("Move this window's title bar to demonstrate screen edge snapping.");
        frame.getContentPane().add(label);
        frame.pack();
        
        frame.addComponentListener(new WindowSnapper());
        frame.setVisible(true);