Pages

Determine when JFrame moved

Source code below will show you, how to determine when a JFrame is moved

*****************************************************************
COMPLETE SOURCE CODE FOR : DetermineWhenAJFrameIsMoved.java
*****************************************************************


import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.event.ComponentEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentListener;

public class DetermineWhenAJFrameIsMoved extends JFrame
{
//Create ComponentListener that will listen when JFrame is moving
ComponentListener cl=new ComponentAdapter()
{
public void componentMoved(ComponentEvent event)
{
//Location that will show is base on top left corner of JFrame
JOptionPane.showMessageDialog(null,"New location with :\n"+"X-coordinate : "+getLocation().getX()+"\n"+"Y-coordinate : "+getLocation().getY());
}
};

public DetermineWhenAJFrameIsMoved()
{
super("TRY MOVE ME");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300,300);
setVisible(true);

//Add ComponentListener to JFrame
addComponentListener(cl);
}

public static void main(String[]args)
{
DetermineWhenAJFrameIsMoved dwajfir=new DetermineWhenAJFrameIsMoved();
}
}


*****************************************************************
JUST COMPILE AND EXECUTE IT
*****************************************************************
Related Posts Plugin for WordPress, Blogger...