Pages

How to know current location of jframe after it moved to other location on screen


Result of the current location for a jframe in this program is current coordinate of the top left corner for the jframe.

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


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

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

import java.awt.Point;
import java.awt.Component;

public class HowToKnowCurrentLocationOfJframe extends ComponentAdapter
{
JFrame b=new JFrame("TRY MOVE ME");

public HowToKnowCurrentLocationOfJframe()
{
b.addComponentListener(this);

b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setSize(400,100);
b.setVisible(true);
}

public void componentMoved(ComponentEvent evt)
{
Component temp=(Component)evt.getSource();
JOptionPane.showMessageDialog(null,"CURRENT LOCATION : "+" X = "+(temp.getLocationOnScreen()).getX()+" Y = "+(temp.getLocationOnScreen()).getY(),"CURRENT LOCATION",JOptionPane.INFORMATION_MESSAGE);
}

public static void main(String[]args)
{
HowToKnowCurrentLocationOfJframe myObject=new HowToKnowCurrentLocationOfJframe();
}
}


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