Java: how to register a listener that listen to a JFrame movement
- by cocotwo
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();
}
}