Event Handling in Java with Examples

Java Programming Examples - Event Handling

An event is a change in the state of an object or its behavior caused by actions such as clicking a button, moving the cursor, pressing a key on the keyboard, and scrolling the page. Events are divided into two categories: foreground events and background events. Event handling is a system that manages events and defines what should happen when an event occurs. Java follows a delegated event model for event handling.

For handling events, the java.awt.event package provides a set of event classes and listener interfaces. The following are some of the most commonly used event classes and listener interfaces in java.awt.event:

Event Classes:

  • ActionEvent
  • AdjustmentEvent
  • ComponentEvent
  • ContainerEvent
  • ItemEvent
  • KeyEvent
  • FocusEvent
  • MouseEvent
  • MouseWheelEvent
  • TextEvent
  • WindowEvent

Listener Interfaces:

  • ActionListener
  • AdjustmentListener
  • ComponentListener
  • ContainerListener
  • ItemListener
  • KeyListener
  • FocusListener
  • MouseListener
  • MouseWheelListener
  • TextListener
  • WindowListener

Java Event Handling Code Approaches:

  1. Within Class
  2. Other Class
  3. Anonymous Class

The following section contains various Java programs on Event Handling like delegation event models, frames, graphics, AWT components, and font classes. It also contains Event Handling programs on AWT components such as buttons, checkboxes, text fields, menu bars, menus, combo boxes, scroll bars, and lists. Each sample program includes a program description, Java code, and program output. All examples have been compiled and tested on Windows and Linux systems.

advertisement
advertisement

1. Java Programs on Delegation Event Model

Program Description
KeyBoardEvent in Java Java Program to Handle KeyBoardEvent
MouseEvent in Java Java Program to Handle MouseEvent

2. Java Programs on Handling Check Boxes & TextField

Program Description
getState(), setState() & getLabel() in Java Java Program to Get and Set State and Get Label of a Check Box
Create Check Boxes & Radio Buttons in Java Java Program to Create Check Boxes and Radio Buttons
Display Text in Frame using Labels in Java Java Program to Display Some Text in the Frame using Labels
Create Text Area & Password Field in Java Java Program to Create Text Area and Password Field

3. Java Programs on Menu Bars and Menus

Program Description
Create Menu & Menu Bar in Java Java Program to Create a Menu and Menu Bar
AWT Choice Class in Java Java Program to Create a Choice Menu to Select an Item
Create Menu & Handle File Open Event in Java Java Program to Create a Menu and Handle the File Open Event
Arithmetic Operations using Switch Case in Java Java Program to Perform Menu Driven Arithmetic Operations using Switch Case

4. Java Programs on AWT Button

Program Description
Create 2 Radio Buttons & Display Selected Button Label in Java Java Program to Create 2 Radio Buttons and Display Selected Button Label
Displaying Image on Clicking JButton in Java Java Program to Create a Button and Display Image in the Frame when Clicked
Create & Switch b/w Frames using Buttons in Java Java Program to Create and Switch between Frames using Buttons
Progress a Progress Bar by 5 Units on Click in Java Java Program to Progress a Progress Bar by 5 Units on Every Click
Create & Set Border to Push Buttons in Java Java Program to Create and Set Border to Push Buttons
Create Toolbar with 3 Image Push Buttons in Java Java Program to Create a Toolbar with 3 Image Push Buttons
Create Split Pane with Two Parts in Java Java Program to Create a Split Pane with Two Parts
Create Buttons & Arrange in Container using Flow Layout Manager in Java Java Program to Create Buttons and Arrange in Container using Flow Layout Manager

5. Java Programs on Combo Box, Scroll Bars & Lists

Program Description
Create Combo Box in Java Java Program to Create a Combo Box
Toggle Button in Java Java Program to Show the Functioning of a Toggle Button
Java AWT Scrollbar Java Program to Create a Vertical Scrollbar with Scroll Button Length 30px
Create List Box in Java Java Program to Create a List Box to Select Multiple Items and Display it in the Frame

6. Java Programs on Frames

Program Description
Display Message in New Frame in Java Java Program to Display a Message in a New Frame
Display Text in Frame using DrawString & Inherit JPanel Class in Java Java Program to Display Text in the Frame by using DrawString and Inheriting JPanel Class
Close Frame using WindowAdapter Class in Java Java Program to Close the Frame using WindowAdapter Class
Close Frame using Anonymous Class in Java Java Program to Close the Frame using an Anonymous Class

advertisement

7. Java Programs on AWT Font Class

Program Description
Program to know which Fonts are Available in System in Java Java Program to Check Which Fonts are Available in a Local System
Display Text in Different Fonts in Java Java Program to Display Text in Different Fonts
Program to Change Component Look & Feel in Java Java Program that Changes the Look and Feel of the Component
JColorChooser Class in Java Java Program to Create a Color Dialog Box to Change the Background Color of Frame
Change Frame Background Color as Cyan in Java Java Program to Change Frame Background Color as Cyan
Change Applet Background Colour when Button Clicked in Java Java Program to Change the Applet Background Colour when Button Clicked

8. Java Programs on Graphics

Program Description
Draw Line using GUI in Java Java Program to Draw a Line using GUI
Display String in Rectangle in Java Java Program to Display String in a Rectangle
Print Concentric Circles in Java Java Program to Print Concentric Circles
Draw a Smiley Face using Graphics in Java Java Program to Draw a Smiling Face using Graphics Class Methods
Display Several Dots on Screen Continuously in Java Java Program to Display Several Dots on the Screen Continuously
JTree Program in Java Java Program to Create a JTree with a Root Node

advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.