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 the ComponentListener 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.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button