Home » Desktop Java » awt » event » Maximization event example

About Byron Kiourtzoglou

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.

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.

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of