Determine when a component moves becomes visible or resizes
In this tutorial we are going to see how to use the ComponentAdapter
class in Java. In some ways the ComponentAdapter
is quite similar to the ComponentListener
interface, but being a class it can be used more robustly, among some other features it implements.
In this example we are going to see how to monitor the window position of a window in you application. The position of the window will be updated every time the user changes position to the window. This may be useful when you want your application to react differently depending on the position of the window.
In short, to print the window position using the ComponentAdapter
, one should follow these steps:
- Create a class that extends
ComponentAdapter
class. - Override the methods that correspond to the events that you want to monitor about the window movement e.g ,
componentMoved
and customize as you wish the handling of the respective events. Now every time the user moves the window, the corresponding method will be executed. - Override the
componentHidden
method. Now every time a component becomes hidden this method will fire up. - Override the
componentResized
method. Now every time a Component gets resized, this method will fire up. - Use the
ComponentEvent.getComponent().getX()
,ComponentEvent.getComponent().getY()
to get the new coordinates of the component that was moved. - Use
addComponentListener(ComponentAdapter adapter)
method to add theComponentAdapter
to the component you wish to monitor.
Let’s take a look at the code snippet that follows:
package com.javacodegeeks.snippets.desktop; import java.awt.Frame; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; public class ComponentListenerExample { public static void main(String[] args) { // Create a frame Frame frame = new Frame("Example Frame"); // Create a listener for component events ComponentListener listener = new ComponentAdapter() { // This method is called only if the component was hidden and setVisible(true) was called public void componentShown(ComponentEvent evt) { Frame c = (Frame) evt.getSource(); System.out.println("Frame : " + c.getTitle() + " is visible ? " + c.isVisible()); } // This method is called only if the component was visible and setVisible(false) was called public void componentHidden(ComponentEvent evt) { Frame c = (Frame) evt.getSource(); System.out.println("Frame : " + c.getTitle() + " is visible ? " + c.isVisible()); } // This method is called after the component's location within its container changes public void componentMoved(ComponentEvent evt) { Frame c = (Frame) evt.getSource(); System.out.println("Frame : " + c.getTitle() + " new location [" + c.getLocation().getX() + "," + c.getLocation().getX() + "]"); } // This method is called after the component's size changes public void componentResized(ComponentEvent evt) { Frame c = (Frame) evt.getSource(); System.out.println("Frame : " + c.getTitle() + " new size [" + c.getSize().getWidth() + "," + c.getSize().getHeight() + "]"); } }; frame.addComponentListener(listener); // Display the frame int frameWidth = 300; int frameHeight = 300; frame.setSize(frameWidth, frameHeight); frame.setVisible(true); } }
Example Output:
Frame : Example Frame new size [300.0,300.0]
Frame : Example Frame new size [300.0,300.0]
Frame : Example Frame is visible ? true
This was an example on how to determine when a component moves, becomes visible or gets resized.