event
Maximization event example
With this tutorial we shall show you how use the ComponentListener
interface to handle Maximization events in your Java Application. The basic idea is very simple. We want the user to be noted every time he maximizes a certain window in our Application. This is very important when your application has to handle many different windows an you want to have full control over re sizing actions.
Basically all you have to do in order to monitor Maximization events is:
- ComponentListener interface
- Override the methods that correspond to the events that you want to monitor about the component e.g
componentMoved
,componentResized
,componentShown
and customize as you wish the handling of the respective events. Now every time the use resizes or moves a component, the corresponding method will be executed - Use
addComponentListener
to add theComponentListener
to the component you wish to monitor.
Let’s take a look at the code:
package com.javacodegeeks.snippets.desktop; import java.awt.Dimension; import java.awt.Frame; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import javax.swing.JFrame; public class Maximize extends JFrame implements ComponentListener { public Maximize() { addComponentListener(this); } @Override public void componentHidden(ComponentEvent event) { System.out.println("Component Hidden"); } @Override public void componentMoved(ComponentEvent event) { System.out.println("Component Moved"); } @Override public void componentResized(ComponentEvent event) { System.out.println("Component Resized"); if (getState() == Frame.ICONIFIED) { System.out.println("Resized to iconified"); } else if (getState() == Frame.NORMAL) { System.out.println("Resized to normal"); } else { System.out.println("Resized to maxomized"); } } @Override public void componentShown(ComponentEvent event) { } public static void main(String[] arg) { Maximize m = new Maximize(); m.setVisible(true); m.setSize(new Dimension(800, 600)); m.setLocation(50, 50); } }
This was an example on how to use ComponentListener to monitor Maximization events in Java.