Java: how to register a listener that listen to a JFrame movement

Posted by cocotwo on Stack Overflow See other posts from Stack Overflow or by cocotwo
Published on 2010-03-11T18:57:54Z Indexed on 2010/03/11 18:59 UTC
Read the original article Hit count: 460

Filed under:
|
|
|

How can you track the movement of a JFrame itself? I'd like to register a listener that would be called back every single time JFrame.getLocation() is going to return a new value.

Here's a skeleton that compiles and runs, what kind of listener should I add so that I can track every JFrame movement on screen?

import javax.swing.*;

public class SO {

    public static void main( String[] args ) throws Exception {
        SwingUtilities.invokeAndWait( new Runnable() {
            public void run() {
                final JFrame jf = new JFrame();
                final JPanel jp = new JPanel();
                final JLabel jl = new JLabel();
                updateText( jf, jl );
                jp.add( jl );
                jf.add( jp );
                jf.pack();
                jf.setVisible( true );
            }
        } );
    }

    private static void updateText( final JFrame jf, final JLabel jl ) {
        jl.setText( "JFrame is located at: " + jf.getLocation() );
        jl.repaint();
    }

}

© Stack Overflow or respective owner

Related posts about java

Related posts about gui