Java Java intro
  1. Java intro
  2. Java basics
Java core
  1. Java variables
  2. Java conditionals
  3. Java loops
  4. Java arrays
  5. Java strings
Object-oriented Java
  1. Java OOP 1
  2. Java OOP 2
  3. Java packages
  4. Java interfaces
Java interactive
  1. Java user input
  2. Java exceptions
  3. Java events
Graphical Java
  1. Java GUI
  2. Java GUI layout
  3. Java graphics
Java extras
  1. Java applets
  2. Java sounds
  3. Java random numbers
Java wrap-up
  1. Java summary

Java event handling

Event handling in Java refers to executing some code when specific things occur such as a window being minimized or a button being clicked.

To demonstrate event handling in action in this tutorial we will be using frames. A frame in Java is a standard graphical window. We will be catching window events like minimize and maximize and performing some action accordingly.

For more information on working with windows and graphics in Java check out our Java GUI, Java GUI layout, and Java graphics pages.

This tutorial focuses on:

Handling events

There are several types of events that can happen in a Java program:

Each event type has it's own interface that you need to implement in a program to handle those events. These interfaces are located in the java.awt.event package.

Each interface has it's own methods to use to execute some code when certain events occur. For example, the KeyListener interface has a keyPressed method that can be used to execute some code when a key is pressed.

Setting up the event functionality

Let's start with a simple frame that will have window events. and then we will add event functionality to it.

import java.awt.*; import java.awt.event.*; class FrameWithEvents implements WindowListener{ }

Now we need to use the methods of the WindowListener interface to specify what happens during window events.

//Window event methods public void windowClosing(WindowEvent e){ System.out.println("The frame is closing....."); } public void windowClosed(WindowEvent e){ .......... } public void windowDeactivated(WindowEvent e){ ............ } ............. .....................

NOTE: When you implement an interface, you have to define all of it's methods in your program.

Making objects listen for events

Now that we implemented the interface and set up the methods we need to specify which component will listen for these events and trigger the functionality accordingly. To do this, we will need to use an event listener. To use an event listener the addWindowListener() method will be used on the component that will listen for these events - the frame.

Like this (entire example below):
aFrame.addWindowListener(this);

An entire frame with events

Here is the code for the entire frame. This frame utilizes all the window event methods. Try it and see how your command prompt will display different messages as you perform actions such as minimize and maximize on the frame.

import java.awt.*; import java.awt.event.*; class FrameWithEvents implements WindowListener{ public FrameWithEvents(){ Frame aFrame = new Frame(); aFrame.setSize(200, 200); aFrame.addWindowListener(this); aFrame.setVisible(true); } public static void main(String[] args){ FrameWithEvents FWE = new FrameWithEvents(); } public void windowClosing(WindowEvent e){ System.out.println("The frame is closing....."); //The following line of code //specifies that the frame should be closed ((Window)e.getSource()).dispose(); } public void windowClosed(WindowEvent e){ System.out.println("The frame has been closed!"); System.exit(0); } public void windowActivated(WindowEvent e){ System.out.println("The frame has been activated"); } public void windowDeactivated(WindowEvent e){ System.out.println("The frame has been deactivated"); } public void windowDeiconified(WindowEvent e){ System.out.println("The frame has been restored from a minimized state"); } public void windowIconified(WindowEvent e){ System.out.println("The frame has been minimized"); } public void windowOpened(WindowEvent e){ System.out.println("The frame is now visible"); } }
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information