Showing posts with label moved. Show all posts
Showing posts with label moved. Show all posts

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
*****************************************************************

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
********************************************************************

RELAXING NATURE VIDEO